檢視原始碼 erl_error 行為 (stdlib v6.2)
此模組提供用於美觀列印錯誤和異常的函式。它被 shell
和 proc_lib
用於列印異常。
引發錯誤的模組可以透過呼叫 error/3
並提供額外的錯誤資訊來提供額外資訊。有關此機制的更多詳細資訊,請參閱 EEP-54。
回呼函式
以下函式將從錯誤資訊處理常式匯出。
摘要
類型
起始欄位編號。預設值為 1。
用於格式化 BIF 和函式呼叫的函式引數的函式。預設情況下,將使用以下函式
帶有格式化選項的 map。
用於修剪堆疊追蹤結尾的函式。它會使用堆疊追蹤中的條目的模組、函式和引數數量來呼叫。如果應該修剪條目,則該函式會回傳 true
,否則回傳 false
。預設值為
回呼
當 format_exception/4
或類似功能想要提供有關錯誤的額外資訊時,會呼叫此回呼。Module
:Function
呼叫是由 error_info
map 指定的。
函式
使用 try
... catch
捕捉的錯誤原因和堆疊回溯格式化,其格式與 shell 格式化它們的格式相同。
類型
-type column() :: pos_integer().
起始欄位編號。預設值為 1。
用於格式化 BIF 和函式呼叫的函式引數的函式。預設情況下,將使用以下函式
fun(Term, I) -> io_lib:print(Term, I, 80, 30) end
-type format_options() :: #{column => column(), stack_trim_fun => stack_trim_fun(), format_fun => format_fun()}.
帶有格式化選項的 map。
用於修剪堆疊追蹤結尾的函式。它會使用堆疊追蹤中的條目的模組、函式和引數數量來呼叫。如果應該修剪條目,則該函式會回傳 true
,否則回傳 false
。預設值為
fun(_, _, _) -> false end
回呼
-callback format_error(Reason, StackTrace) -> ErrorDescription when Reason :: term(), StackTrace :: erlang:stacktrace(), ArgumentPosition :: pos_integer(), ErrorDescription :: #{ArgumentPosition => unicode:chardata(), general => unicode:chardata(), reason => unicode:chardata()}.
當 format_exception/4
或類似功能想要提供有關錯誤的額外資訊時,會呼叫此回呼。Module
:Function
呼叫是由 error_info
map 指定的。
該函式應回傳一個 map,其中包含有關導致異常的原因的額外資訊。map 的可能鍵值是
ArgumentPosition = pos_integer()
- 導致錯誤的引數位置,從 1 開始。general
- 與任何引數無關的錯誤導致了此錯誤。reason
- 如果Reason
應該以與預設方式不同的方式列印。
如果回傳的文字包含換行符號,format_exception/4
會正確縮排文字。
範例
-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
函式
-spec format_exception(Class, Reason, StackTrace) -> unicode:chardata() when Class :: error | exit | throw, Reason :: term(), StackTrace :: erlang:stacktrace().
等效於 format_exception/4
。
-spec format_exception(Class, Reason, StackTrace, Options) -> unicode:chardata() when Class :: error | exit | throw, Reason :: term(), StackTrace :: erlang:stacktrace(), Options :: format_options().
使用 try
... catch
捕捉的錯誤原因和堆疊回溯格式化,其格式與 shell 格式化它們的格式相同。
範例
try
do_something()
catch
C:R:Stk ->
Message = erl_error:format_exception(C, R, Stk),
io:format(LogFile, "~ts\n", [Message])
end
如果異常提供了 error_info
,format_exception
將使用該資訊來提供有關異常的額外資訊。
範例
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
有關如何引發包含 error_info
的異常的詳細資訊,請參閱 erlang:error/3
。