erl_error (stdlib v3.15.2)
This module provides functions for pretty-printing errors and exceptions. It is used by both the shell
and by proc_lib
to print exceptions.
It is possible for the module raising an error to provide additional information by calling error/3
with extra error information. More details about this mechanism is described in EEP-54.
Callback Functions
The following functions are to be exported from an Error Info handler.
Link to this section Summary
Types
Start column number. Default is 1.
A fun used to format function arguments for BIF and function calls. By default the following fun will be used
A map with formatting options.
A fun used to trim the end of the stacktrace. It is called with module, function, and arity from an entry from the stacktrace. The fun is to return true
if the entry should be trimmed, and false
otherwise. The default value is
Functions
Format the error reason and stack back-trace caught using try
... catch
in the same style as the shell formats them.
Link to this section Types
-type column() :: term().
Specs
column() :: pos_integer().
Start column number. Default is 1.
-type format_fun() :: term().
Specs
format_fun() :: fun((term(), column()) -> iolist()).
A fun used to format function arguments for BIF and function calls. By default the following fun will be used:
fun(Term, I) -> io_lib:print(Term, I, 80, 30) end
-type format_options() :: term().
Specs
format_options() :: #{column => column(), stack_trim_fun => stack_trim_fun(), format_fun => format_fun()}.
A map with formatting options.
-type stack_trim_fun() :: term().
Specs
stack_trim_fun() :: fun((module(), atom(), arity()) -> boolean()).
A fun used to trim the end of the stacktrace. It is called with module, function, and arity from an entry from the stacktrace. The fun is to return true
if the entry should be trimmed, and false
otherwise. The default value is:
fun(_, _, _) -> false end
Link to this section Functions
Specs
format_exception(Class, Reason, StackTrace) -> unicode:chardata() when Class :: error | exit | throw, Reason :: term(), StackTrace :: erlang:stacktrace().
Format the error reason and stack back-trace caught using try
... catch
in the same style as the shell formats them.
Example:
try
do_something()
catch
C:R:Stk ->
Message = erl_error:format_exception(C, R, Stk),
io:format(LogFile, "~ts\n", [Message])
end
If error_info
is provided with the exception, format_exception
will use that information to provide additional information about the exception.
Example:
try
erlang:raise(badarg,[],[{error_info,#{}}])
catch
C:R:Stk ->
Message = erl_error:format_exception(C, R, Stk),
io:format(LogFile, "~ts\n", [Message])
end
See erlang:error/3
for details on how to raise an exception with error_info
included.
Specs
format_exception(Class, Reason, StackTrace, Options) -> unicode:chardata() when Class :: error | exit | throw, Reason :: term(), StackTrace :: erlang:stacktrace(), Options :: format_options().