Hi guys!
Want to open here another discussion about error handling concepts in Elixir because it looks like I really stuck here by myself.
My question is about errors that are related to users interactions, where you can’t just “Fail fast and forget / Let it crash” but need to respond with some appropriate error message.
Elixir community’s standard is to return tuples like {:error, reason}
when something goes wrong during function execution. Overall, it looks good at the first sight.
But at some point, I noticed that to handle this approach I have to write tons of case
or with
really often just to check that underlying call didn’t return this {:error, reason}
tuple.
Consider an example of a multilayered application (like Controller → Business Logic → database CRUDs). If there is some expected error that occurs on the layer of CRUDs, I will need to have a case
to catch this error on a Business Logic layer and pass it to Controller. After that, I need to have another case
on the Controller’s layer. And it becomes even worse if you have more than 3 layers there, or your business logic includes something more than just a call to CRUD layer.
Maybe I really missed something but it seems to me that this approach makes you write a lot of boilerplate.
I’m actually from Java world and it would be quite natural for me to resolve this issue by throwing (raising) an exception from the CRUDs level and catch in on the Controller level. But if I understand correctly, exceptions in Elixir philosophy are more about unexpected errors that should be handled by this “Let it crash” way.
My question still is: is it normal and common way to write elixir code having this error checks (case/with) on every layer of the application?
I can provide some naive code example if my thoughts are not clear.