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 c47348f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 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

0 comments on commit c47348f

Please sign in to comment.