I know we can get the full stacktrace using __STACKTRACE__
in a catch/rescue block in elixir, but what's the correct way of printing it? In my case, I rescue from an error but I still want to log it to console. This is what I'm doing right now:
def unreliable_method(item) do
# Do something with `item`
:ok
rescue
_err ->
Logger.error("Failed for item: #{inspect(item)}")
Logger.error(inspect(__STACKTRACE__))
{:error, :processing_failed}
end
This was answered by Michał Muskała on the official elixir github issue:
The canonical way would be to use
Exception.format_stacktrace/1
From the docs, it's clear that we don't need to explicitly pass __STACKTRACE__
as an argument to the method when still inside the rescue
block. It's automatically retrieved using Process.info
:
Logger.error(Exception.format_stacktrace())
Michal's comment helped me find Exception.format/3
, that formats the error itself and its complete stacktrace, and that seemed more appropriate for my usecase:
def unreliable_method do
# do something
:ok
rescue
err ->
Logger.error(Exception.format(:error, err, __STACKTRACE__))
{:error, :processing_failed}
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With