Qqwy
TypeCheck Core Team
Error handling on a list: How to propagate {:error, _} tuples?
Today I found myself writing the following piece of code source in context:
def from_list(other_users_json_list) do
other_users_json_list
|> reduce_errors(&from_json_hash/1)
end
defp reduce_errors(enumerable, function) do
enumerable
|> Enum.reduce({:ok, []}, fn
{:error, error}, _ -> {:error, error}
{:ok, list}, elem ->
case function.(elem) do
{:ok, elem} -> [elem | list]
{:error, error} -> {:error, error}
end
end)
end
It tries to turn a list of maps that was parsed before from JSON into a list of structs, returning either {:ok, list_of_structs} or {:error, some_validation_error}.
It should stop parsing as soon as the first error is found. However, the only way I know of that propagates an ok/error-tuple through a list of changes is the above, which feels like an awful lot of manual labour and not very idiomatic Elixir-code.
But I cannot be the only one that has faced this type of problem: Are there other idioms of handling these situations?
Most Liked
gregvaughn
If I’m understanding correctly, I think Enum.reduce_while would fit nicely here. Caution: code has not been executed
Enum.reduce_while(enumerable, {:ok, []}, fn elem, {:ok, acc} ->
case function.(elem) do
{:ok, new_elem} -> {:cont, {:ok, [new_elem | acc]}}
{:error, _} = err -> {:halt, err}
end
end)
9
Popular in Questions
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it.
I’m very interested in Elixir,...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
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
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Other popular topics
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New








