Two musings about stacktrace functionality

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:

      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.

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.