ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - CrowdHailer/OK: Elegant error/exception handling in Elixir, with result monads.
Elegant error/exception handling in Elixir, with result monads. - CrowdHailer/OK
Visit Site

GitHub - CrowdHailer/OK: Elegant error/exception handling in Elixir, with result monads.

GitHub - CrowdHailer/OK: Elegant error/exception handling in Elixir, with result monads.

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 several 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 several 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 Pipe

The pipe (~>>) is equivalent to bind/flat_map. The pipe (~>) is equivalent to map.

These macros allows pipelining result tuples through multiple functions for an extremely concise happy path.

use OK.Pipe

def get_employee_data(file, name) do
  {:ok, file}
  ~>> File.read
  ~> String.upcase
end

Use ~>> for File.read because it returns a result tuple. Use ~> for String.upcase because it returns a bare value that should be wrapped in an ok tuple.

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: 1, failure: 1]

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

FAQ

Why does OK not catch raised errors?

For the main rational behind this decision see the article Errors are not exceptional

Two other 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
    

What changed in version 2.0

  • OK.with was deprecated.
  • use OK.Pipe was added.
  • OK.bind was renamed OK.flat_map.

Additional External Links and Resources

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory