mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add When Things Don't Match The With Statements as an elixir til
This commit is contained in:
30
elixir/when-things-dont-match-the-with-statements.md
Normal file
30
elixir/when-things-dont-match-the-with-statements.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# When Things Don't Match The With Statements
|
||||
|
||||
You set up a series of match statements in a `with` construct as a way of
|
||||
avoiding a bunch of nested if statements. Inevitably you will be passing
|
||||
data through that doesn't meet all of the match criteria. By default, the
|
||||
`with` construct will short circuit and your program will continue from
|
||||
there.
|
||||
|
||||
You can, however, take more control over how you handle the _failure_ cases
|
||||
by employing an `else` block. The `else` block works a lot like a case
|
||||
statement.
|
||||
|
||||
```elixir
|
||||
with %{status_code: 200, body: body} <- HTTPoison.get!(url),
|
||||
{:ok, decoded_body} <- Poison.decode(body) do
|
||||
{:ok, decoded_body}
|
||||
else
|
||||
%{status_code: 401} ->
|
||||
reauthenticate()
|
||||
_ ->
|
||||
log_error()
|
||||
end
|
||||
```
|
||||
|
||||
Here we are able to anticipate a _failure_ case and respond accordingly. For
|
||||
everything else, we have a generic action that we take.
|
||||
|
||||
See the [docs for
|
||||
`with`](https://hexdocs.pm/elixir/Kernel.SpecialForms.html#with/1) for more
|
||||
details.
|
||||
Reference in New Issue
Block a user