Exception

Convenience functions to work with and pretty print exceptions and stacktraces.

Notice that stacktraces in Elixir are updated on errors. For example, at any given moement, System.stacktrace will return the stacktrace for the last error that ocurred in the current process.

That said, many of the functions in this module will automatically calculate the stacktrace based on the caller, when invoked without arguments, changing the value of System.stacktrace. If instead you want to format the stacktrace of the latest error, you should instead explicitly pass the System.stacktrace as argument.

Source

Summary

format_fa(fun, arity)

Receives an anonymous function and arity and formats it as shown in stacktraces. The arity may also be a list of arguments

format_file_line(file, line)

Formats the given file and line as shown in stacktraces. If any of the values are nil, they are omitted

format_mfa(module, fun, arity)

Receives a module, fun and arity and formats it as shown in stacktraces. The arity may also be a list of arguments

format_stacktrace(trace \\ nil)

Formats the stacktrace

format_stacktrace_entry(entry)

Receives an stacktrace entry and formats it into a string

normalize(exception)

Normalizes an exception, converting Erlang exceptions to Elixir exceptions

normalize(arg1, exception)

Normalizes an exception, converting Erlang exceptions to Elixir exceptions

Types

stacktrace_entry :: {module, function, arity_or_args, location} | {function, arity_or_args, location}

Functions

format_fa(fun, arity)

Receives an anonymous function and arity and formats it as shown in stacktraces. The arity may also be a list of arguments.

Examples

Exception.format_fa(fn -> end, 1)
#=> "#Function<...>/1"
Source
format_file_line(file, line)

Formats the given file and line as shown in stacktraces. If any of the values are nil, they are omitted.

Examples

iex> Exception.format_file_line("foo", 1)
"foo:1:"

iex> Exception.format_file_line("foo", nil)
"foo:"

iex> Exception.format_file_line(nil, nil)
""
Source
format_mfa(module, fun, arity)

Receives a module, fun and arity and formats it as shown in stacktraces. The arity may also be a list of arguments.

Examples

iex> Exception.format_mfa Foo, :bar, 1
"Foo.bar/1"
iex> Exception.format_mfa Foo, :bar, []
"Foo.bar()"
iex> Exception.format_mfa nil, :bar, []
"nil.bar()"

Anonymous functions are reported as -func/arity-anonfn-count-, where func is the name of the enclosing function. Convert to "anonymous fn in func/arity"

Source
format_stacktrace(trace \\ nil)

Formats the stacktrace.

A stacktrace must be given as an argument. If not, this function calculates a new stacktrace based on the caller and formats it. As a consequence, the value of System.stacktrace is changed.

Source
format_stacktrace_entry(entry)

Specs:

Receives an stacktrace entry and formats it into a string.

Source
normalize(exception)

Normalizes an exception, converting Erlang exceptions to Elixir exceptions.

Useful when interfacing Erlang code with Elixir code.

Source
normalize(arg1, exception)

Normalizes an exception, converting Erlang exceptions to Elixir exceptions.

It takes the kind spilled by catch as an argument and normalizes only :error, returning the untouched payload for others.

Source