stacktrace_compat

stacktrace_compat is a workaround for the deprecation of :get_stacktrace() in Erlang/OTP 21.

It intends on smoothing near-future maintenance of projects that are to support both pre- and post-deprecation code by avoiding code duplication or ungainly macros.

Getting Started

1. Import as dependency

rebar.config:

{deps,
 [% [...]
  {stacktrace_compat, "1.2.1"}
 ]}.
2. Apply transform when compiling modules

rebar.config:

{erl_opts,
 [% [...]
  {parse_transform, stacktrace_transform}
 ]}.

Example Transformation

The following snippet:

foobar() ->
    try
        1 / (rand:uniform(2) - 1)
    catch
        error:badarith ->
            {error, {badarith, erlang:get_stacktrace()}}
    end.

...would be transformed into:

foobar() ->
    try
        1 / (rand:uniform(2) - 1)
    catch
        error:badarith:StacktraceCompat444353487_1 ->
            {error, {badarith, StacktraceCompat444353487_1}}
    end.

Tested setup

Details

stacktrace_compat defines a parse transform (stacktrace_transform) which, when applied to modules on OTP 21+, will replace erlang:get_stacktrace() calls with instances of the stacktrace binding that is to be captured on the closest catch pattern up the abstract syntax tree (within the same named function.)

If no binding has been defined, a generated name will be used that's likely to be conflict free.

If no catch pattern is found, no replacement is made. This makes sense in naked calls to erlang:get_stacktrace() (they're a no-no anyway) but not in calls from within exception helpers, i.e. code that grabs the stacktrace from within a function which is not the same as the one from which the exception had been thrown - it's a limitation yet to be worked out (if at all.)

Generated by EDoc