How to log the error object?

I’m not sure how to log the “error” object that might be returned from a function call. I know that IO.puts can only work with strings and that IO.inspect can handle a struct, but I was trying to do an error message with string interpolation that also includes information from the error variable passed back by the caller, which I assume is a struct that has a bunch of useful information, like stacktfrace, etc.

My solution so far is to just log two lines, one with identifying info using string interpolation and another with just the error object, in the hope that Logger.error will work with it. I’m not sure how people handle the “else” case in a pattern match block and would appreciate advice if I’m doing it the right way or not (see “yellow arrows” in image, below). Thanks!

Use Kernel — Elixir v1.14.0

You may be interested in Untangle - logging and inspecting with code location information

2 Likes

I usually do Logger.error("Error while creating lookup, details: #{inspect error}") the inspect will gonna take care of error struct.

1 Like