25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Get stacktrace out of lager context

Calling erlang:get_stacktrace() inside lager functions produces
stacktraces of the logging function itself, not the function which has failed.
This commit is contained in:
Evgeniy Khramtsov 2018-09-01 19:37:26 +03:00
parent b416527e4f
commit 88d0b71d58
11 changed files with 39 additions and 22 deletions

View File

@ -151,7 +151,8 @@ get_certificates(Domains) ->
throw:Throw -> throw:Throw ->
Throw; Throw;
E:R -> E:R ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), St = erlang:get_stacktrace(),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]),
{error, get_certificates} {error, get_certificates}
end; end;
false -> false ->
@ -243,7 +244,8 @@ get_certificate(CAUrl, DomainName, PrivateKey) ->
throw:Throw -> throw:Throw ->
Throw; Throw;
E:R -> E:R ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), St = erlang:get_stacktrace(),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]),
{error, DomainName, get_certificate} {error, DomainName, get_certificate}
end. end.
@ -382,7 +384,8 @@ renew_certificates() ->
throw:Throw -> throw:Throw ->
Throw; Throw;
E:R -> E:R ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), St = erlang:get_stacktrace(),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]),
{error, get_certificates} {error, get_certificates}
end. end.
@ -447,7 +450,8 @@ list_certificates(Verbose) ->
throw:Throw -> throw:Throw ->
Throw; Throw;
E:R -> E:R ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), St = erlang:get_stacktrace(),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]),
{error, list_certificates} {error, list_certificates}
end; end;
false -> false ->
@ -489,7 +493,8 @@ format_certificate(DataCert, Verbose) ->
end end
catch catch
E:R -> E:R ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), St = erlang:get_stacktrace(),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]),
fail_format_certificate(DomainName) fail_format_certificate(DomainName)
end. end.
@ -614,7 +619,8 @@ revoke_certificates(DomainOrFile) ->
throw:Throw -> throw:Throw ->
Throw; Throw;
E:R -> E:R ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), St = erlang:get_stacktrace(),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]),
{error, revoke_certificate} {error, revoke_certificate}
end. end.
@ -1118,7 +1124,8 @@ save_certificate({ok, DomainName, Cert}) ->
throw:Throw -> throw:Throw ->
Throw; Throw;
E:R -> E:R ->
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, erlang:get_stacktrace()]), St = erlang:get_stacktrace(),
?ERROR_MSG("Unknown ~p:~p, ~p", [E, R, St]),
{error, DomainName, saving} {error, DomainName, saving}
end. end.

View File

@ -380,11 +380,12 @@ safe_apply(Hook, Module, Function, Args) ->
apply(Module, Function, Args) apply(Module, Function, Args)
end end
catch E:R when E /= exit; R /= normal -> catch E:R when E /= exit; R /= normal ->
St = get_stacktrace(),
?ERROR_MSG("Hook ~p crashed when running ~p:~p/~p:~n" ?ERROR_MSG("Hook ~p crashed when running ~p:~p/~p:~n"
"** Reason = ~p~n" "** Reason = ~p~n"
"** Arguments = ~p", "** Arguments = ~p",
[Hook, Module, Function, length(Args), [Hook, Module, Function, length(Args),
{E, R, get_stacktrace()}, Args]), {E, R, St}, Args]),
'EXIT' 'EXIT'
end. end.

View File

@ -71,8 +71,9 @@ start_link() ->
route(Packet) -> route(Packet) ->
try do_route(Packet) try do_route(Packet)
catch E:R -> catch E:R ->
St = erlang:get_stacktrace(),
?ERROR_MSG("failed to route packet:~n~s~nReason = ~p", ?ERROR_MSG("failed to route packet:~n~s~nReason = ~p",
[xmpp:pp(Packet), {E, {R, erlang:get_stacktrace()}}]) [xmpp:pp(Packet), {E, {R, St}}])
end. end.
-spec route_iq(iq(), function()) -> ok. -spec route_iq(iq(), function()) -> ok.

View File

@ -91,8 +91,9 @@ start_link() ->
route(Packet) -> route(Packet) ->
try do_route(Packet) try do_route(Packet)
catch E:R -> catch E:R ->
St = erlang:get_stacktrace(),
?ERROR_MSG("failed to route packet:~n~s~nReason = ~p", ?ERROR_MSG("failed to route packet:~n~s~nReason = ~p",
[xmpp:pp(Packet), {E, {R, erlang:get_stacktrace()}}]) [xmpp:pp(Packet), {E, {R, St}}])
end. end.
-spec route(jid(), jid(), xmlel() | stanza()) -> ok. -spec route(jid(), jid(), xmlel() | stanza()) -> ok.

View File

@ -122,10 +122,11 @@ row_to_route(Domain, {ServerHost, NodeS, PidS, LocalHintS} = Row) ->
catch _:{bad_node, _} -> catch _:{bad_node, _} ->
[]; [];
E:R -> E:R ->
St = erlang:get_stacktrace(),
?ERROR_MSG("failed to decode row from 'route' table:~n" ?ERROR_MSG("failed to decode row from 'route' table:~n"
"Row = ~p~n" "Row = ~p~n"
"Domain = ~s~n" "Domain = ~s~n"
"Reason = ~p", "Reason = ~p",
[Row, Domain, {E, {R, erlang:get_stacktrace()}}]), [Row, Domain, {E, {R, St}}]),
[] []
end. end.

View File

@ -95,8 +95,9 @@ start_link() ->
route(Packet) -> route(Packet) ->
try do_route(Packet) try do_route(Packet)
catch E:R -> catch E:R ->
St = erlang:get_stacktrace(),
?ERROR_MSG("failed to route packet:~n~s~nReason = ~p", ?ERROR_MSG("failed to route packet:~n~s~nReason = ~p",
[xmpp:pp(Packet), {E, {R, erlang:get_stacktrace()}}]) [xmpp:pp(Packet), {E, {R, St}}])
end. end.
clean_temporarily_blocked_table() -> clean_temporarily_blocked_table() ->

View File

@ -142,9 +142,10 @@ route(Packet) ->
Packet1 -> Packet1 ->
try do_route(Packet1), ok try do_route(Packet1), ok
catch E:R -> catch E:R ->
St = erlang:get_stacktrace(),
?ERROR_MSG("failed to route packet:~n~s~nReason = ~p", ?ERROR_MSG("failed to route packet:~n~s~nReason = ~p",
[xmpp:pp(Packet1), [xmpp:pp(Packet1),
{E, {R, erlang:get_stacktrace()}}]) {E, {R, St}}])
end end
end. end.

View File

@ -114,8 +114,9 @@ process_iq(_Host, Module, Function, IQ) ->
ignore -> ignore ->
ok ok
catch E:R -> catch E:R ->
St = erlang:get_stacktrace(),
?ERROR_MSG("failed to process iq:~n~s~nReason = ~p", ?ERROR_MSG("failed to process iq:~n~s~nReason = ~p",
[xmpp:pp(IQ), {E, {R, erlang:get_stacktrace()}}]), [xmpp:pp(IQ), {E, {R, St}}]),
Txt = <<"Module failed to handle the query">>, Txt = <<"Module failed to handle the query">>,
Err = xmpp:err_internal_server_error(Txt, IQ#iq.lang), Err = xmpp:err_internal_server_error(Txt, IQ#iq.lang),
ejabberd_router:route_error(IQ, Err) ejabberd_router:route_error(IQ, Err)

View File

@ -193,7 +193,8 @@ process([Call], #request{method = 'POST', data = Data, ip = IPPort} = Req) ->
?DEBUG("Bad Request: ~p", [_Err]), ?DEBUG("Bad Request: ~p", [_Err]),
badrequest_response(<<"Invalid JSON input">>); badrequest_response(<<"Invalid JSON input">>);
_:_Error -> _:_Error ->
?DEBUG("Bad Request: ~p ~p", [_Error, erlang:get_stacktrace()]), St = erlang:get_stacktrace(),
?DEBUG("Bad Request: ~p ~p", [_Error, St]),
badrequest_response() badrequest_response()
end; end;
process([Call], #request{method = 'GET', q = Data, ip = {IP, _}} = Req) -> process([Call], #request{method = 'GET', q = Data, ip = {IP, _}} = Req) ->
@ -210,8 +211,8 @@ process([Call], #request{method = 'GET', q = Data, ip = {IP, _}} = Req) ->
throw:{error, unknown_command} -> throw:{error, unknown_command} ->
json_format({404, 44, <<"Command not found.">>}); json_format({404, 44, <<"Command not found.">>});
_:_Error -> _:_Error ->
St = erlang:get_stacktrace(),
?DEBUG("Bad Request: ~p ~p", [_Error, erlang:get_stacktrace()]), ?DEBUG("Bad Request: ~p ~p", [_Error, St]),
badrequest_response() badrequest_response()
end; end;
process([_Call], #request{method = 'OPTIONS', data = <<>>}) -> process([_Call], #request{method = 'OPTIONS', data = <<>>}) ->
@ -314,7 +315,8 @@ handle(Call, Auth, Args, Version) when is_atom(Call), is_list(Args) ->
throw:Msg when is_list(Msg); is_binary(Msg) -> throw:Msg when is_list(Msg); is_binary(Msg) ->
{400, iolist_to_binary(Msg)}; {400, iolist_to_binary(Msg)};
_Error -> _Error ->
?ERROR_MSG("REST API Error: ~p ~p", [_Error, erlang:get_stacktrace()]), St = erlang:get_stacktrace(),
?ERROR_MSG("REST API Error: ~p ~p", [_Error, St]),
{500, <<"internal_error">>} {500, <<"internal_error">>}
end; end;
{error, Msg} -> {error, Msg} ->

View File

@ -2815,9 +2815,9 @@ process_item_change(Item, SD, UJID) ->
undefined -> undefined ->
<<"">> <<"">>
end, end,
St = erlang:get_stacktrace(),
?ERROR_MSG("failed to set item ~p~s: ~p", ?ERROR_MSG("failed to set item ~p~s: ~p",
[Item, FromSuffix, [Item, FromSuffix, {E, {R, St}}]),
{E, {R, erlang:get_stacktrace()}}]),
{error, xmpp:err_internal_server_error()} {error, xmpp:err_internal_server_error()}
end. end.

View File

@ -321,8 +321,9 @@ process_iq_get(#iq{to = To, lang = Lang,
ver = Version} ver = Version}
end) end)
catch E:R -> catch E:R ->
St = erlang:get_stacktrace(),
?ERROR_MSG("failed to process roster get for ~s: ~p", ?ERROR_MSG("failed to process roster get for ~s: ~p",
[jid:encode(To), {E, {R, erlang:get_stacktrace()}}]), [jid:encode(To), {E, {R, St}}]),
Txt = <<"Roster module has failed">>, Txt = <<"Roster module has failed">>,
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang)) xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang))
end. end.