Skip to content

Commit

Permalink
Move to a separate header file and rename to SAFELY
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Aug 6, 2023
1 parent 9505758 commit 25fa007
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
7 changes: 0 additions & 7 deletions include/mongoose.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@

-define(MONGOOSE_URI, <<"https://www.erlang-solutions.com/products/mongooseim.html">>).

-define(APPLY_SAFELY(F),
try F catch
error:R:S -> {exception, #{class => error, reason => R, stacktrace => S}};
throw:R -> {exception, #{class => throw, reason => R}};
exit:R:S -> {exception, #{class => exit, reason => R, stacktrace => S}}
end).

%% ---------------------------------
%% Logging mechanism
-include("mongoose_logger.hrl").
Expand Down
11 changes: 11 additions & 0 deletions include/safely.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-ifndef(SAFELY).
-define(SAFELY, true).

-define(SAFELY(F),
try F catch
error:R:S -> {exception, #{class => error, reason => R, stacktrace => S}};
throw:R -> {exception, #{class => throw, reason => R}};
exit:R:S -> {exception, #{class => exit, reason => R, stacktrace => S}}
end).

-endif.
3 changes: 2 additions & 1 deletion src/gen_hook.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

-ignore_xref([start_link/0, add_handlers/1, delete_handlers/1]).

-include("safely.hrl").
-include("mongoose.hrl").

-type hook_name() :: atom().
Expand Down Expand Up @@ -227,7 +228,7 @@ run_hook([Handler | Ls], Acc, Params, Key) ->
hook_fn_ret() | safely:exception().
apply_hook_function(#hook_handler{hook_fn = HookFn, extra = Extra},
Acc, Params) ->
?APPLY_SAFELY(HookFn(Acc, Params, Extra)).
?SAFELY(HookFn(Acc, Params, Extra)).

error_running_hook(Info, Handler, Acc, Params, Key) ->
?LOG_ERROR(Info#{what => hook_failed,
Expand Down
6 changes: 3 additions & 3 deletions src/safely.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
%% If it is an error, we want the stacktrace, if not, we don't.
%% For more information, see usage in test/safely_SUITE.erl

-include("mongoose.hrl").
-include("safely.hrl").
-include_lib("kernel/include/logger.hrl").

-ifdef(TEST).
Expand Down Expand Up @@ -40,11 +40,11 @@
-ifdef(TEST).
-spec apply(fun((...) -> A), [term()]) -> A | exception().
apply(Function, Args) when is_function(Function), is_list(Args) ->
?APPLY_SAFELY(erlang:apply(Function, Args)).
?SAFELY(erlang:apply(Function, Args)).

-spec apply(atom(), atom(), [term()]) -> term() | exception().
apply(Module, Function, Args) when is_atom(Function), is_list(Args) ->
?APPLY_SAFELY(erlang:apply(Module, Function, Args)).
?SAFELY(erlang:apply(Module, Function, Args)).
-endif.

-spec apply_and_log(fun((...) -> A), [term()], map()) -> A | exception().
Expand Down
16 changes: 9 additions & 7 deletions test/safely_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-module(safely_SUITE).
-compile([export_all, nowarn_export_all]).
-include("safely.hrl").
-include_lib("eunit/include/eunit.hrl").

all() ->
Expand All @@ -12,29 +13,30 @@ all() ->
handles_errors_similar_to_catch(_) ->
{SafeRes, CatchRes} =
%% These two must be on the same line for the stacktraces to be equal.
{safely:apply(lists, min, [[]]),(catch apply(lists, min, [[]]))},
{?SAFELY(lists:min([])), catch lists:min([])},

{exception, #{class := error, reason := function_clause, stacktrace := SafeST}} = SafeRes,
{'EXIT', {function_clause, CatchST}} = CatchRes,

true = (hd(CatchST) == hd(SafeST)),

{safely,apply,3,[{file,_},{line,_}]} = hd(tl(SafeST)), % this is extra in SafeStackTrace
% {safely,apply,3,[{file,_},{line,_}]} = hd(tl(SafeST)), % this is extra in SafeStackTrace
{lists,min,[[]],[{file,_},{line,_}]} = hd(SafeST), % this is extra in SafeStackTrace

true = (tl(CatchST) == tl(tl(SafeST))).
true = (tl(CatchST) == tl(SafeST)).

handles_exits_similar_to_errors(_) ->
ExitF = fun() -> exit(i_quit) end,
{exception, #{class := exit, reason := i_quit, stacktrace := _S}} = safely:apply(ExitF,[]),
{exception, #{class := exit, reason := i_quit, stacktrace := _S}} = ?SAFELY(ExitF()),
{'EXIT', i_quit} = (catch apply(ExitF,[])),
ok.

handles_throws_unlike_catch(_) ->
ThrowF = fun() -> throw(up) end,
{exception, #{class := throw, reason := up}} = safely:apply(ThrowF,[]),
{exception, #{class := throw, reason := up}} = ?SAFELY(ThrowF()),
up = (catch apply(ThrowF,[])),
ok.

handles_success_like_catch(_) ->
1 = safely:apply(lists, min, [[1,2,3]]),
1 = (catch apply(lists, min, [[1,2,3]])).
1 = ?SAFELY(lists:min([1,2,3])),
1 = (catch lists:min([1,2,3])).

0 comments on commit 25fa007

Please sign in to comment.