Skip to content

Instantly share code, notes, and snippets.

@marocchino
Created November 15, 2016 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marocchino/2157d050e99cce4ffe72ee487f2d90be to your computer and use it in GitHub Desktop.
Save marocchino/2157d050e99cce4ffe72ee487f2d90be to your computer and use it in GitHub Desktop.
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