-
-
Save marocchino/2157d050e99cce4ffe72ee487f2d90be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
result = case File.read("my_company/employees.json") do | |
{:ok, contents} -> | |
case Poison.decode(contents) do | |
{:ok, json} -> | |
case Dict.fetch(json, "Peter") do | |
{:ok, data} -> {:ok, data} | |
:error -> {:error, :key_not_found} | |
# Notice the Dict API doesn't return error with reason. | |
# So we add a reason to make it conform. | |
end | |
{:error, reason} -> {:error, reason} | |
end | |
{:error, reason} -> {:error, reason} | |
end | |
result = | |
with {:ok, contents} <- File.read("my_company/employees.json"), | |
{:ok, json} <- Poison.decode(contents), | |
{:ok, data} <- Dict.fetch(json, "Peter") do | |
{:ok, data} | |
else | |
{:error, reason} -> {:error, reason} | |
:error -> {:error, :key_not_found} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment