浏览代码

Handle errors generated by cowboy which were treated as ranch errors

c998673eb0/src/cowboy_stream_h.erl (L153-L157)
pull/485/head
Grigory Starinkin 6 年前
父节点
当前提交
5cbc1260fc
找不到此签名对应的密钥 GPG 密钥 ID: 6A32D1F8AB4C166A
共有 2 个文件被更改,包括 27 次插入0 次删除
  1. +7
    -0
      src/error_logger_lager_h.erl
  2. +20
    -0
      test/lager_test_backend.erl

+ 7
- 0
src/error_logger_lager_h.erl 查看文件

@ -244,6 +244,13 @@ log_event(Event, #state{sink=Sink} = State) ->
%% Ranch errors
?CRASH_LOG(Event),
case Args of
%% Error logged by cowboy, which starts as ranch error
[Ref, ConnectionPid, StreamID, RequestPid, Reason, StackTrace] ->
{Md, Formatted} = format_reason_md({Reason, StackTrace}),
?LOGFMT(Sink, error, [{pid, RequestPid} | Md],
"Cowboy stream ~p with ranch listener ~p and connection process ~p "
"had its request process exit with reason: ~s",
[StreamID, Ref, ConnectionPid, Formatted]);
[Ref, _Protocol, Worker, {[{reason, Reason}, {mfa, {Module, Function, Arity}}, {stacktrace, StackTrace} | _], _}] ->
{Md, Formatted} = format_reason_md({Reason, StackTrace}),
?LOGFMT(Sink, error, [{pid, Worker} | Md],

+ 20
- 0
test/lager_test_backend.erl 查看文件

@ -1648,6 +1648,26 @@ error_logger_redirect_test_() ->
?assertEqual("Cowboy handler my_handler terminated with reason: call to undefined function my_handler:to_json/2", lists:flatten(Msg))
end
},
{"Cowboy error reports, 6 arg version",
fun(Sink) ->
Stack = [{app_http, init, 2, [{file, "app_http.erl"}, {line,9}]},
{cowboy_handler, execute, 2, [{file, "cowboy_handler.erl"}, {line, 41}]}],
ConnectionPid = list_to_pid("<0.82.0>"),
sync_error_logger:error_msg(
"Ranch listener ~p, connection process ~p, stream ~p "
"had its request process ~p exit with reason "
"~999999p and stacktrace ~999999p~n",
[my_listner, ConnectionPid, 1, self(), {badmatch, 2}, Stack]),
_ = gen_event:which_handlers(error_logger),
{Level, _, Msg, Metadata} = pop(Sink),
?assertEqual(lager_util:level_to_num(error), Level),
?assertEqual(self(), proplists:get_value(pid, Metadata)),
?assertEqual("Cowboy stream 1 with ranch listener my_listner and "
"connection process <0.82.0> had its request process exit "
"with reason: no match of right hand value 2 "
"in app_http:init/2 line 9", lists:flatten(Msg))
end
},
{"messages should not be generated if they don't satisfy the threshold",
fun(Sink) ->
lager:set_loglevel(Sink, ?MODULE, undefined, error),

正在加载...
取消
保存