Skip to content

Commit 495833f

Browse files
authored
Merge pull request #130 from stritzinger/otp-21-stacktrace-compatibility
Add compatibility for OTP 21+ stack traces
2 parents 46bf9f2 + 9d20a5a commit 495833f

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/riak_ensemble_exchange.erl

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
-compile(nowarn_export_all).
2323
-compile(export_all).
2424

25+
-include("stacktrace.hrl").
26+
2527
start_exchange(Ensemble, Peer, Id, Tree, Peers, Views, Trusted) ->
2628
spawn(fun() ->
2729
try
2830
perform_exchange(Ensemble, Peer, Id, Tree, Peers, Views, Trusted)
29-
catch Class:Reason ->
30-
io:format("CAUGHT: ~p/~p~n~p~n", [Class, Reason, erlang:get_stacktrace()]),
31+
catch ?_exception_(Class, Reason, StackToken) ->
32+
io:format("CAUGHT: ~p/~p~n~p~n", [Class, Reason, ?_get_stacktrace_(StackToken)]),
3133
gen_fsm_compat:send_event(Peer, exchange_failed)
3234
end
3335
end).

src/stacktrace.hrl

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
%% Originating from Quviq AB
2+
%% Fix to make Erlang programs compile on both OTP20 and OTP21.
3+
%%
4+
%% Get the stack trace in a way that is backwards compatible. Luckily
5+
%% OTP_RELEASE was introduced in the same version as the new preferred way of
6+
%% getting the stack trace. A _catch_/2 macro is provided for consistency in
7+
%% cases where the stack trace is not needed.
8+
%%
9+
%% Example use:
10+
%% try f(...)
11+
%% catch
12+
%% ?_exception_(_, Reason, StackToken) ->
13+
%% case Reason of
14+
%% {fail, Error} -> ok;
15+
%% _ -> {'EXIT', Reason, ?_get_stacktrace_(StackToken)}
16+
%% end
17+
%% end,
18+
19+
-ifdef(OTP_RELEASE). %% This implies 21 or higher
20+
-define(_exception_(Class, Reason, StackToken), Class:Reason:StackToken).
21+
-define(_get_stacktrace_(StackToken), StackToken).
22+
-define(_current_stacktrace_(),
23+
try
24+
exit('$get_stacktrace')
25+
catch
26+
exit:'$get_stacktrace':__GetCurrentStackTrace ->
27+
__GetCurrentStackTrace
28+
end).
29+
-else.
30+
-define(_exception_(Class, Reason, _), Class:Reason).
31+
-define(_get_stacktrace_(_), erlang:get_stacktrace()).
32+
-define(_current_stacktrace_(), erlang:get_stacktrace()).
33+
-endif.

0 commit comments

Comments
 (0)