OK
Elegant error handling in Elixir, with result monads.
Result tuples
The OK module works with result tuples by treating them as a result monad.
{:ok, value} | {:error, reason}
See Handling Errors in Elixir for a more detailed explanation.
OK.with
OK.with/1
allows for more concise and ultimately more readable code than the native with
construct. It does this by leveraging result monads for both the happy and non-happy paths. By extracting the actual function return values from the result tuples, OK.with/1
reduces noise which improves readability and recovers precious horizontal code real estate. This also encourages writing idiomatic Elixir functions which return :ok
/:error
tuples.
- Elegant error handling with result monads, alternative to elixir
with
special form - Discussion on :ok/:error
Basic Usage
- Use the
<-
operator to match & extract a value for an:ok
tuple. - Use the
=
operator as you normally would for pattern matching an untagged result. - Return result must also be in the form of a tagged tuple.
- Optionally pattern match on some errors in an
else
block.
NB: Statements are not delimited by commas as with the native Elixir with
construct.
require OK
OK.with do
user <- fetch_user(1) # `<-` operator means func returns {:ok, user}
cart <- fetch_cart(1) # `<-` again, {:ok, cart}
order = checkout(cart, user) # `=` allows pattern matching on non-tagged funcs
save_order(order) # Returns an ok/error tuple
end
The cart example above is equivalent to the following nested case
statements
case fetch_user(1) do
{:ok, user} ->
case fetch_cart(1) do
{:ok, cart} ->
order = checkout(cart, user)
save_order(order)
{:error, reason} ->
{:error, reason}
end
{:error, reason} ->
{:error, reason}
end
You can pattern match on errors as well in an else
block:
require OK
OK.with do
user <- fetch_user(1)
cart <- fetch_cart(1)
order = checkout(cart, user)
save_order(order)
else
:user_not_found -> # Match on untagged reason
{:error, :unauthorized} # Return a literal error tuple
end
Note that the else
block pattern matches on the extracted error reason, but the return expression must still be the full tuple.
Unlike Elixir’s native with
construct, any unmatched error case does not throw an error and will just be passed as the return value
You can also use OK.success
and OK.failure
macros:
require OK
OK.with do
user <- fetch_user(1)
cart <- fetch_cart(1)
order = checkout(cart, user)
saved <- save_order(order)
OK.success saved
else
:user_not_found ->
OK.failure :unauthorized
end
Result Pipeline Operator
~>>
This macro allows pipelining result tuples through multiple functions for an extremely concise happy path.
The ~>>
macro is equivalent to bind/flat_map in other languages.
import OK only: ["~>>": 2]
def get_employee_data(file, name) do
{:ok, file}
~>> File.read
~>> Poison.decode
~>> Dict.fetch(name)
end
Semantic matches
OK
provides macros for matching on success and failure cases.
This allows for code to check if a result returned from a function was a success or failure.
This check can be done without knowledge about how the result is structured to represent a success or failure
import OK, only: [success: 2, failure: 2]
case fetch_user(id) do
success(user) ->
user
failure(:not_found) ->
create_guest_user()
end
Additional External Links and Resources
Elixir Forum
- Railway programming
Similar Libraries