README.md

# stacktrace\_compat

[![](https://img.shields.io/hexpm/v/stacktrace_compat.svg?style=flat)](https://hex.pm/packages/stacktrace_compat)
[![](https://travis-ci.org/g-andrade/stacktrace_compat.png?branch=master)](https://travis-ci.org/g-andrade/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:

``` erlang
{deps,
 [% [...]
  {stacktrace_compat, "1.2.0"}
 ]}.
```

##### 2\. Apply transform when compiling modules

rebar.config:

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

#### Example Transformation

The following snippet:

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

...would be transformed into:

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

#### Tested setup

  - Erlang/OTP 19 or higher
  - rebar3

#### 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*