BT

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

Bookmarks

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

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.