# Two musings about stacktrace functionality

**URL:** <https://forum.elixirforum.com/t/two-musings-about-stacktrace-functionality/72922>\
**Category:** Discussions\
**Tags:** stacktrace\
**Created:** [October 15, 2025, 4:16am UTC](https://forum.elixirforum.com/t/two-musings-about-stacktrace-functionality/72922 "2025-10-15T04:16:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oliver](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/oliver/32/1167_2.png) [@Oliver](https://forum.elixirforum.com/u/Oliver)\
**Post date:** [October 15, 2025, 4:16am UTC](https://forum.elixirforum.com/t/two-musings-about-stacktrace-functionality/72922/1 "2025-10-15T04:16:52Z")

</div>

Currently I have in my project two ways to make a stack trace:

One function to be used in try-blocks piping ` __STACKTRACE__ ` into `Exception.format_stacktrace/1` and then into the Logger.

And one that calls uses `Process.info/2` and then formats that with `Exception.format_stacktrace_entry/1` similar to `Exception.format_stacktrace/1` without arguments.

The reasoning is this: The implementation of `Exception.format_stacktrace/1` is like this:

```elixir
      if trace do
        trace
      else
        case Process.info(self(), :current_stacktrace) do
          {:current_stacktrace, t} -> Enum.drop(t, 3)
        end
      end

```

In all cases where I tried to use it, it shaved off the immediate caller (like the module used for callbacks in a GenServer) and left me with “the uninteresting part” (the stack trace elements pertaining to `:gen_server`, for example). Might it not be better to do a `tl(t)` here? (Then it just removes the entry for `Process.info/2`.)

The other thing is about the ` __STACKTRACE__ ` special form. It is not usable outside catch/rescue. But wouldn’t it be handier if it just returned `nil` in all other contexts?

Then you could always pipe it into `Exception.format_stacktrace/1` and reuse the same code everywhere.

I admit in my case it would be a minor improvement, but currently the user has to always decide which of my two stack trace functions to call.

---

<div class="post-metadata">

**Author:** ![Oliver](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/oliver/32/1167_2.png) [@Oliver](https://forum.elixirforum.com/u/Oliver)\
**Post date:** [October 15, 2025, 11:01am UTC](https://forum.elixirforum.com/t/two-musings-about-stacktrace-functionality/72922/2 "2025-10-15T11:01:50Z")

</div>

Ah, sorry, I just redid my code and realized that one of these two was my bad:

I called `Exception.format_stacktrace/1` from inside a macro’s quote block which changes the stacktrace. Now that I converted my code into a function I see why the first three entries were removed.
