2020 2021 Ericsson AB, All Rights Reserved Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. The Initial Developer of the Original Code is Ericsson AB. erl_error Bjorn Gustavsson Kenneth Lundin 1 2020-10-16 A erl_error.xml
erl_error Utilities for reporting errors.

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.

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:

fun(_, _, _) -> false end

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

Start column number. Default is 1.

Format an exception.

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.

Callback Functions

The following functions are to be exported from an Error Info handler.

Module:format_error(Reason, StackTrace) -> ErrorDescription Callback for formatting exceptions Reason = term() StackTrace = erlang:stacktrace() ArgumentPosition = pos_integer() ErrorDescription =   #{ ArgumentPosition => unicode:chardata(),      general => unicode:chardata(),      reason => unicode:chardata() }

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:

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