Skip to content

Commit

Permalink
chore: create exception reports lazily (#35)
Browse files Browse the repository at this point in the history
* chore: create exception reports lazily

* more verbose
  • Loading branch information
chsukivra authored Oct 7, 2021
1 parent 6dfffb2 commit c8676dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
16 changes: 4 additions & 12 deletions include/prelude.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,12 @@
-define(emergency(StringOrReport, ArgsOrMeta), ?LOG_EMERGENCY(StringOrReport, ArgsOrMeta)).
-define(emergency(FunOrFormat, Args, Meta), ?LOG_EMERGENCY(FunOrFormat, Args, Meta)).

-define(failed(Rsn, Extras)
, ?error( #{ reason => Rsn
, extras => s2_util:ensure_map(Extras)})).
-define(failed(Rsn)
, ?failed(Rsn, [])).
-define(failed(Rsn, Extras), ?LOG_ERROR(fun s2_util:report_failed/1, [Rsn, Extras])).
-define(failed(Rsn), ?failed(Rsn, #{})).

-define(exception(Class, Reason, Stacktrace, Extras)
, ?error(#{ class => Class
, reason => Reason
, stacktrace => Stacktrace
, extras => s2_util:ensure_map(Extras)})).
-define(exception(C, R, S, Extras), ?LOG_ERROR(fun s2_util:report_exception/1, [C, R, S, Extras])).
-define(exception(Class, Reason, Stacktrace), ?exception(Class, Reason, Stacktrace, #{})).

-define(exception(Class, Reason, Stacktrace)
, ?exception(Class, Reason, Stacktrace, [])).
-else.

-define(failed(Rsn, Extras), ?error( "Error: ~p"
Expand Down
27 changes: 27 additions & 0 deletions src/s2_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
-export([ consult_string/1
, init_folsom/1
, ensure_map/1
, report_failed/1
, report_exception/1
]).

-ignore_xref([init_folsom/1]).
Expand Down Expand Up @@ -69,6 +71,31 @@ ensure_map(Map) when is_map(Map) ->
ensure_map(List) ->
maps:from_list(List).

-spec report_failed( list() ) -> map().
report_failed([Reason, Extras]) ->
#{ reason => Reason
, extras => ensure_map(Extras)
}.

-spec report_exception( list() ) -> map().
report_exception([Class, Reason, Stacktrace, Extras]) ->
#{ reason => Reason
, exception_class => Class
, stacktrace => stacktrace_to_map(Stacktrace)
, extras => ensure_map(Extras)
}.

stacktrace_to_map([]) ->
[];
stacktrace_to_map([{Module, Function, Arity, Props}|Rest]) ->
Frame = #{ module => Module
, function => Function
, arity => Arity
, file => proplists:get_value(file, Props)
, line => proplists:get_value(line, Props)
},
[Frame | stacktrace_to_map(Rest)].


%%%_* Emacs ============================================================
%%% Local Variables:
Expand Down

0 comments on commit c8676dd

Please sign in to comment.