sc4224
Universal error handler for elixir applications
Hi not sure what the best practice for handling errors are in elixir. Is there an example of how I can make a universal error handler to take care of any type of error and return an error message for my absinthe graphql elixir application?
Most Liked
easco
There is no such thing as a “Universal error handler”. An Elixir application is a collection of processes and those processes don’t share memory or processing threads except through explicit message passing. That includes error information. The best you could do is put a top level exception handler on a process but then the question becomes what do you do with that error?
The correct answer is that you let the process crash and allow a supervisor to decide how to deal with the failure. In a Goto conference presentation, Joe Armstrong (creator of Erlang) described it as (paraphrasing) “What do I do if I have a heart attack? Do I run off to find a defribulator? No, I ask someone else to deal with the error”
In short, the idea of a “universal error handler” runs pretty much counter to the design philosophy of Erlang (and by extension Elixir) so you are probably barking up the wrong tree. If you are looking for a good catch-phrase to learn more about handling errors in Elixir an Erlang, search for “Let it Crash” along with Erlang and Elixir.
hauleth
Because it is compile time warning which is not handled by the logger at all. If you want to prevent something like that, then you should use --warnings-as-errors during compilation (or set that flag as a default in your mix.exs).
Fl4m3Ph03n1x
I believe the OP is asking about errors and not exceptions.
- Errors are something we can predict in our apps, like a user entering a wrong password.
- Exceptions are the heart attacks you mentioned. We didn’t consider for a meteor to fall, so when it does we allow our processes to crash and have a supervisor take care of them.
hauleth
Yes, it would. However if you want to handle only errors (for example like Sentry or BugSnag handler) then it will be better to attach yourself to logger instead as it will give you structured data which will be easier to transform to whatever you need. However beware that this is not overload protected, so you should handle that on your own.







