Types
Reason = term()
ArgumentPosition = pos_integer()
ErrorDescription =
This callback is called when
format_exception/4 or similar functionality wants to provide
extra information about an error. The Module:Function called
is the one specificed by the error_info map.
The function should return a map with additional information about what
have caused the exception. The possible keys of the map are:
- ArgumentPosition = pos_integer()
- The position of the argument that caused the error starting at 1.
- general
- An error that is not associated with any argument caused the error.
- reason
- If the Reason should be printed differently than the default way.
If the text returned includes new-lines,
format_exception/4 will
indent the text correctly.
Example:
-module(my_error_module).
-export([atom_to_string/1, format_error/2]).
atom_to_string(Arg) when is_atom(Arg) ->
atom_to_list(Arg);
atom_to_string(Arg) ->
erlang:error(badarg,[Arg],
[{error_info,#{ module => ?MODULE,
cause => #{ 1 => "should be an atom" }}}]).
format_error(Reason, [{_M,_F,_As,Info}|_]) ->
ErrorInfo = proplists:get_value(error_info, Info, #{}),
ErrorMap = maps:get(cause, ErrorInfo),
ErrorMap#{ general => "optional general information",
reason => io_lib:format("~p: ~p",[?MODULE, Reason]) }.
1> c(my_error_module).
{ok,my_error_module}
2> my_error_module:atom_to_string(1).
** exception error: my_error_module: badarg
in function my_error_module:atom_to_string/1
called as my_error_module:atom_to_string(1)
*** argument 1: should be an atom
*** optional general information