summaryrefslogtreecommitdiff
path: root/lib/stdlib/doc/src/erl_error.xmlsrc
blob: 2800fff1ed475e5883e7b97727d65cacaec13328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>2020</year>
      <year>2021</year>
      <holder>Ericsson AB, All Rights Reserved</holder>
    </copyright>
    <legalnotice>
  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.
    </legalnotice>

    <title>erl_error</title>
    <prepared>Bjorn Gustavsson</prepared>
    <responsible>Kenneth Lundin</responsible>
    <docno>1</docno>
    <approved></approved>
    <checked></checked>
    <date>2020-10-16</date>
    <rev>A</rev>
    <file>erl_error.xml</file>
  </header>
  <module since="OTP 24.0">erl_error</module>
  <modulesummary>Utilities for reporting errors.
  </modulesummary>

  <description>
    <p>This module provides functions for pretty-printing errors and exceptions. It is used
      by both the <seeerl marker="stdlib:shell"><c>shell</c></seeerl> and by
      <seeerl marker="proc_lib"><c>proc_lib</c></seeerl> to print exceptions.</p>
    <p>It is possible for the module raising an error to provide additional
      information by calling <seemfa marker="erts:erlang#error/3"><c>error/3</c></seemfa>
      with extra error information. More details about this mechanism is described
      in <url href="https://www.erlang.org/erlang-enhancement-proposals/eep-0054.html">EEP-54</url>.</p>
  </description>

  <datatypes>
    <datatype>
      <name name="format_options"/>
      <desc><p>A map with formatting options.</p></desc>
    </datatype>
    <datatype>
      <name name="stack_trim_fun"/>
      <desc><p>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 <c>true</c> if the entry
      should be trimmed, and <c>false</c> otherwise. The default value
      is:</p>
      <pre>
fun(_, _, _) -> false end</pre>
      </desc>
    </datatype>
    <datatype>
      <name name="format_fun"/>
      <desc><p>A fun used to format function arguments for BIF and function calls.
      By default the following fun will be used:</p>
      <pre>
fun(Term, I) -> io_lib:print(Term, I, 80, 30) end</pre>
      </desc>
    </datatype>
    <datatype>
      <name name="column"/>
      <desc><p>Start column number. Default is 1.</p></desc>
    </datatype>
  </datatypes>

  <funcs>
    <func>
      <name name="format_exception" arity="3" since="OTP 24.0"/>
      <name name="format_exception" arity="4" since="OTP 24.0"/>
      <fsummary>Format an exception.</fsummary>
      <desc>
        <p>Format the error reason and stack back-trace caught using
          <c>try</c> ... <c>catch</c> in the same style as the shell
	  formats them.</p>
	<p>Example:</p>
	<code type="erl">
try
    do_something()
catch
    C:R:Stk ->
        Message = erl_error:format_exception(C, R, Stk),
        io:format(LogFile, "~ts\n", [Message])
end</code>
        <p>If <c>error_info</c> is provided with the exception,
          <c>format_exception</c> will use that information to provide
          additional information about the exception.</p>
        <p>Example:</p>
        <code type="erl">
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</code>
        <p>See <seemfa marker="erts:erlang#error/3"><c>erlang:error/3</c></seemfa>
          for details on how to raise an exception with <c>error_info</c> included.</p>
      </desc>
    </func>
  </funcs>

  <section>
    <title>Callback Functions</title>
    <p>The following functions are to be exported from an Error Info handler.</p>
  </section>

  <funcs>
    <func>
      <name anchor="format_error" since="OTP 24.0">Module:format_error(Reason, StackTrace) -> ErrorDescription</name>
      <fsummary>Callback for formatting exceptions</fsummary>
      <type>
        <v>Reason = term()</v>
        <v>StackTrace = <seetype marker="erts:erlang#stacktrace">erlang:stacktrace()</seetype></v>
        <v>ArgumentPosition = pos_integer()</v>
        <v>ErrorDescription = </v>
        <v>&nbsp;&nbsp;#{ ArgumentPosition => <seetype marker="unicode#chardata">unicode:chardata()</seetype>, </v>
        <v>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;general => <seetype marker="unicode#chardata">unicode:chardata()</seetype>,</v>
        <v>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reason => <seetype marker="unicode#chardata">unicode:chardata()</seetype> }</v>
      </type>
      <desc>
        <p>This callback is called when <seemfa marker="#format_exception/4"><c>
          format_exception/4</c></seemfa> or similar functionality wants to provide
          extra information about an error. The <c>Module</c>:<c>Function</c> called
          is the one specificed by the <c>error_info</c> map.</p>
          <p>The function should return a map with additional information about what
            have caused the exception. The possible keys of the map are:</p>
          <taglist>
            <tag><c>ArgumentPosition = pos_integer()</c></tag>
            <item>The position of the argument that caused the error starting at 1.</item>
            <tag><c>general</c></tag>
            <item>An error that is not associated with any argument caused the error.</item>
            <tag><c>reason</c></tag>
            <item>If the <c>Reason</c> should be printed differently than the default way.</item>
          </taglist>
          <p>If the text returned includes new-lines,
          <seemfa marker="#format_exception/4"><c>format_exception/4</c></seemfa> will
          indent the text correctly.</p>
          <p>Example:</p>
          <codeinclude file="../../examples/my_error_module.erl" tag="%% example" type="erl"></codeinclude>
        <pre>
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</pre>
      </desc>
    </func>
  </funcs>
</erlref>