pmjoe
What is the most idiomatic way to handle fine grained errors in Elixir?
The {:error, _} is quite common and used everywhere, but what this is telling me is that an error happened, and nothing more. Errors usually have way more context attached to it. Maybe it was an user request error, in this case I’ll not log an entry on the APM, or maybe it was an error where I can retry in a few milliseconds, you got the gist of it.
What is the best way to encode more information at the errors?
{:error, :error_type, .... }? But then this can be a pattern match nightmare because errors could have a dynamic quantity of fields. Maybe {:error, %{extra details here}}?
Marked As Solved
LostKobrakai
A common approach taken by libraries like mint and others is normalizing {:error, exceptions_struct}, which allows you to hold more data in regards to a given error, but still allow for normalized handling through {:error, _} as well as all the functionality of the Exception protocol across multiple error sources.
Also Liked
gregvaughn
I have used this {:error, exception_struct} approach to good effect in an order processing system. Not only could I have a field in the exception struct to specify the “step” of order processing that failed, but the exception module itself served as a nice place for a mitigate function to centralize knowledge of what can be retried, what should just be logged, what should go into a manual human-monitored error queue, etc.
D4no0
Do you want the small error details at your application level or logging? There are big differences and implications between one or the other. If you are trying to write defensive code that handles all errors at application layer, then you will not have many tools to help you out, as that opposes the idea of OTP to let it crash.
If we are talking about logging, then this problem is already solved. The logger formatter allows to specify your custom format of the logs. The great additional feature it has it’s called metadata:
$metadata- user controlled data presented in"key=val key2=val2 "format
In your codebase you can use Logger.metadata/1 to set all the custom metadata you might require when something gets logged, the great thing is that this is also automatically applied to when errors are logged, so you basically don’t have to do anything custom.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









