Skip to content

nurges/OK

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OK

Elegant error/exception handling in Elixir, with result monads.

Hex pm Build Status License

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.

See FAQ at end of README for a few common question.

OK.for

OK.for/1 combines serveral functions that may fail.

  • 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.
require OK

OK.for 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
  saved_order <- save_order(order)
after
  saved_order                       # Value will be wrapped if not already a result tuple
end

OK.for/1 guarantees that it's return value is also in the structure of a result tuple.

OK.try

OK.try/1 combines serveral functions that may fail, and handles errors.

This is useful when writing code that has it's own representation of errors. e.g. HTTP Responses.

For example when using raxx to build responses the following code will always return a response.

require OK
import Raxx

OK.try 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
  saved_order <- save_order(order)
after
  response(:created)                # Value will be returned unwrapped
rescue
  :user_not_found ->
    response(:not_found)
  :could_not_save ->
    response(:internal_server_error)
end

OK Pipeline Operator

The OK pipeline macro (~>>) is equivalent to bind/flat_map in other languages, and this allows pipelining result tuples through multiple functions for an extremely concise happy path.

import OK, only: ["~>>": 2]

def get_employee_data(file, name) do
  {:ok, file}
  ~>> File.read
  ~>> Poison.decode
  ~>> Dict.fetch(name)
end

OK.with

This macro is deprecated. Use instead OK.try/1 or OK.for/1

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.

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 inside OK.with blocks 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

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 while hiding implementation details about how that result is structured.

import OK, only: [success: 2, failure: 2]

case fetch_user(id) do
  success(user) ->
    user
  failure(:not_found) ->
    create_guest_user()
end

FAQ

Why does OK not catch raised errors?

Two reasons:

  • Exceptional input and errors are not the same thing, OK leaves raising exceptions as a way to handle errors that should never happen.
  • Calls inside try/1 are not tail recursive since the VM needs to keep the stacktrace in case an exception happens. see source.

What about other shapes of error and success?

  • Accepting any extra forms is a slippery slope, and they are not always unambiguous. If a library is not returning errors as you like it is very easy to wrap in a custom function.

    def fetch_foo(map) do
      case Map.fetch(map, :foo) do
        {:ok, foo} -> {:ok, foo}
        :error -> {:error, :no_foo}
      end
    end

Additional External Links and Resources