cowboy_error_h.erl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. %% Copyright (c) 2014, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(cowboy_error_h).
  15. -behaviour(gen_event).
  16. %% Public interface.
  17. -export([ignore/3]).
  18. %% gen_event.
  19. -export([init/1]).
  20. -export([handle_event/2]).
  21. -export([handle_call/2]).
  22. -export([handle_info/2]).
  23. -export([terminate/2]).
  24. -export([code_change/3]).
  25. %% Public interface.
  26. %% Ignore crashes from Pid occuring in M:F/A.
  27. ignore(M, F, A) ->
  28. gen_event:call(error_logger, ?MODULE, {expect, self(), M, F, A}).
  29. %% gen_event.
  30. init(_) ->
  31. {ok, []}.
  32. %% Ignore supervisor and progress reports.
  33. handle_event({info_report, _, {_, progress, _}}, State) ->
  34. {ok, State};
  35. handle_event({info_report, _, {_, std_info, _}}, State) ->
  36. {ok, State};
  37. handle_event({error_report, _, {_, supervisor_report, _}}, State) ->
  38. {ok, State};
  39. %% Ignore gun retry failures.
  40. handle_event({error_report, _, {_, crash_report,
  41. [[{initial_call, {gun, init, _}}, _, _,
  42. {error_info, {error, gone, _}}|_]|_]}},
  43. State) ->
  44. {ok, State};
  45. %% Ignore emulator reports, they are a duplicate of what Ranch gives us.
  46. handle_event({error, _, {emulator, _, _}}, State) ->
  47. {ok, State};
  48. handle_event(Event = {error, GL,
  49. {_, "Ranch listener" ++ _, [_, _, Pid, {[_, _,
  50. {stacktrace, [{M, F, A, _}|_]}|_], _}]}},
  51. State) when node(GL) =:= node() ->
  52. A2 = if is_list(A) -> length(A); true -> A end,
  53. Crash = {Pid, M, F, A2},
  54. case lists:member(Crash, State) of
  55. true ->
  56. {ok, lists:delete(Crash, State)};
  57. false ->
  58. write_event(Event),
  59. {ok, State}
  60. end;
  61. handle_event(Event = {_, GL, _}, State) when node(GL) =:= node() ->
  62. write_event(Event),
  63. {ok, State};
  64. handle_event(_, State) ->
  65. {ok, State}.
  66. handle_call({expect, Pid, M, F, A}, State) ->
  67. {ok, ok, [{Pid, M, F, A}|State]};
  68. handle_call(_, State) ->
  69. {ok, {error, bad_query}, State}.
  70. handle_info(_, State) ->
  71. {ok, State}.
  72. terminate(_, _) ->
  73. ok.
  74. code_change(_, State, _) ->
  75. {ok, State}.
  76. write_event(Event) ->
  77. error_logger_tty_h:write_event(
  78. {erlang:universaltime(), Event},
  79. io).