jberling

jberling

How to ensure {:error, _} tuples are handled and not silently ignored?

First, sorry if this has already been asked in other topics. I couldn’t find it, but I would guess it’s a common thing to handle.

Let’s say I create a function that returns :ok or {:error, cause} — it’s a function that focuses on side effects. If I forget to handle the error (match the return value), the error will effectively be swallowed silently. How do you remember to handle the error? Or is this a bad pattern? Maybe the function should just raise an error instead?

It would be nice to catch this at compile time, but I understand it’s hard to do since Elixir is dynamic. However, Elixir is gradually becoming more statically typed. Will it be possible (or is it already possible) to check this statically?

Most Liked

LostKobrakai

LostKobrakai

The idea behind such a return pattern is that the function doesn’t want to decide how errors are to be handled, but delegates this responsibility to the caller of the function. So as you eluded to it’s the responsibility of the caller to deal with handling errors.

If you just do my_function() then indeed it could succeed or fail and nobody will be informed of either. Personally I’d argue ignoring return values is most often a code smell. It should likely be :ok = my_function() to raise in any non successful cases, or you could use case or with to deal with the error somehow.

There’s also a setting for dialyzer to warn if return values are not matched on forcing _ = my_function() for the cases, where indeed the return value is to be ignored.

13
Post #3
dimitarvp

dimitarvp

Yeah, this 100%. Elixir is not a statically-typed language and we can’t use sum types either.

Best we can do is make sure the code crashes early.

One very curious psychological effect of this is that people suddenly become good programmers again: “We can’t just assume a successful result!” – same people who were perfectly OK with just ignoring the return value.

So I found :ok = do_stuff() an amazing psychological hack: you make people pay attention and they also want proper error handling.

FlyingNoodle

FlyingNoodle

If your program should crash you can assert like you suggested or you write a ! version that raises.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
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
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement