pmjoe

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

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

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

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.

Where Next?

Popular in Questions Top

New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement