BT

Elixir 1.7 Improves Error Handling, Logging, and Testing

| by Sergio De Simone Follow 18 Followers on Aug 06, 2018. Estimated reading time: 2 minutes |

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 Stage
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.

Tell us what you think

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

Email me replies to any of my messages in this thread
Community comments

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

Email me replies to any of my messages in this thread

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

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT