BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Elixir 1.7 Improves Error Handling, Logging, and Testing

Elixir 1.7 Improves Error Handling, Logging, and Testing

This item in japanese

Elixir 1.7 is focused on quality of life improvements, writes Elixir creator José Valim. Those include a new __STACKTRACE__ construct to retrieve the stacktrace, integration with Erlang’s new :logger module, improvements to Elixir’s unit testing library ExUnit, and support for metadata in documentation.

Elixir 1.7 exception system has been improved by making the ArgumentError, ArithmeticError, and KeyError exceptions provide more diagnostic information and adding a new __STACKTRACE__ construct that can be used instead of System.stacktrace/0 to retrieve the stacktrace:

try do
  ... something that may fail ...
rescue
  exception ->
    log(exception, __STACKTRACE__)
    reraise(exception, __STACKTRACE__)
end

__STACKTRACE__ is lexically scoped and does not rely on side-effects, unlike System.stacktrace/0. According to Valim, this could bring performance improvements in a future Elixir release since it can make it not necessary anymore to keep track of stacktraces after the try block ends.

The Elixir Logger module now plugs into Erlang’s :logger, which is available with Erlang/OTP 21, and fully leverages the latter’s richer metadata, including:

  • :crash_reason, a two-element tuple with the throw/error/exit reason as first argument and the stacktrace as second. A throw will always have the structure {:nocatch, term}, while errors will always be Exceptions, and exits cover the rest of cases.

  • :initial_call, the initial call that started the process.

  • :registered_name, the process registered name as an atom.

Additionally, Logger macros like debug, info, etc., will not evaluate their arguments unless the message is effectively logged, and a new :compile_time_purge_matching option enables filtering log entries based on compile-time metadata. For example, this is how you configure Logger ignore log calls from application :foo with level lower than :info, as well all logger calls from Bar.foo/3:

config :logger,
  compile_time_purge_matching: [
    [application: :foo, level_lower_than: :info],
    [module: Bar, function: "foo/3"]
  ]

As mentioned, Elixir’s unit testing library ExUnit has also been improved by making the assert macro returns more detailed information. For example, if the assert some_function(expr1, var2) statement fails, it will print out the values of the arguments to some_function, thus sparing the developer the effort to re-run the test to inspect their values.

As a final note, Elixir 1.7 also introduces the possibility of annotating documentation with metadata, such as in the following example:

@moduledoc "A brand new module"
@moduledoc authors: ["Jane", "Mary"], since: "1.4.0"

The ExDoc tool in Elixir 1.7 is able to leverage those metadata to flag the use of deprecated modules, functions, callbacks, and types, and to show when a given feature was introduced.

Do not miss the official release notes for full detail about the changes in Elixir 1.7.

Rate this Article

Adoption
Style

BT