{"id":13507857,"url":"https://github.com/CrowdHailer/OK","last_synced_at":"2025-03-30T09:33:09.544Z","repository":{"id":45469403,"uuid":"44631565","full_name":"CrowdHailer/OK","owner":"CrowdHailer","description":"Elegant error/exception handling in Elixir, with result monads.","archived":false,"fork":false,"pushed_at":"2020-01-13T09:05:34.000Z","size":122,"stargazers_count":604,"open_issues_count":4,"forks_count":20,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-24T20:06:16.725Z","etag":null,"topics":["elixir","elixir-pipelines","macros","monad","pipeline"],"latest_commit_sha":null,"homepage":"http://insights.workshop14.io/2015/10/18/handling-errors-in-elixir-no-one-say-monad.html","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CrowdHailer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-20T19:58:33.000Z","updated_at":"2025-03-23T15:44:30.000Z","dependencies_parsed_at":"2022-07-15T01:46:52.629Z","dependency_job_id":null,"html_url":"https://github.com/CrowdHailer/OK","commit_stats":null,"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrowdHailer%2FOK","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrowdHailer%2FOK/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrowdHailer%2FOK/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CrowdHailer%2FOK/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CrowdHailer","download_url":"https://codeload.github.com/CrowdHailer/OK/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246301963,"owners_count":20755512,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","elixir-pipelines","macros","monad","pipeline"],"created_at":"2024-08-01T02:00:41.180Z","updated_at":"2025-03-30T09:33:09.280Z","avatar_url":"https://github.com/CrowdHailer.png","language":"Elixir","funding_links":[],"categories":["Errors and Exception Handling","Elixir","Macros"],"sub_categories":[],"readme":"# OK\n\n**Elegant error/exception handling in Elixir, with result monads.**\n\n[![Hex pm](http://img.shields.io/hexpm/v/ok.svg?style=flat)](https://hex.pm/packages/ok)\n[![Build Status](https://secure.travis-ci.org/CrowdHailer/OK.svg?branch=master\n\"Build Status\")](https://travis-ci.org/CrowdHailer/OK)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)\n\n- [Install from Hex](https://hex.pm/packages/ok)\n- [Documentation available on hexdoc](https://hexdocs.pm/ok)\n\n## Result tuples\n\nThe OK module works with result tuples by treating them as a result monad.\n\n```elixir\n{:ok, value} | {:error, reason}\n```\n\nSee [Handling Errors in Elixir](http://insights.workshop14.io/2015/10/18/handling-errors-in-elixir-no-one-say-monad.html) for a more detailed explanation.\n\nSee [FAQ](#faq) at end of README for a few common question.\n\n## OK.for\n\n`OK.for/1` combines several functions that may fail.\n\n- Use the `\u003c-` operator to match \u0026 extract a value for an `:ok` tuple.\n- Use the `=` operator as you normally would for pattern matching an untagged result.\n\n```elixir\nrequire OK\n\nOK.for do\n  user \u003c- fetch_user(1)             # `\u003c-` operator means func returns {:ok, user}\n  cart \u003c- fetch_cart(1)             # `\u003c-` again, {:ok, cart}\n  order = checkout(cart, user)      # `=` allows pattern matching on non-tagged funcs\n  saved_order \u003c- save_order(order)\nafter\n  saved_order                       # Value will be wrapped if not already a result tuple\nend\n```\n\n`OK.for/1` guarantees that it's return value is also in the structure of a result tuple.\n\n## OK.try\n\n`OK.try/1` combines several functions that may fail, and handles errors.\n\nThis is useful when writing code that has it's own representation of errors.\ne.g. HTTP Responses.\n\nFor example when using raxx to build responses the following code will always return a response.\n\n```elixir\nrequire OK\nimport Raxx\n\nOK.try do\n  user \u003c- fetch_user(1)             # `\u003c-` operator means func returns {:ok, user}\n  cart \u003c- fetch_cart(1)             # `\u003c-` again, {:ok, cart}\n  order = checkout(cart, user)      # `=` allows pattern matching on non-tagged funcs\n  saved_order \u003c- save_order(order)\nafter\n  response(:created)                # Value will be returned unwrapped\nrescue\n  :user_not_found -\u003e\n    response(:not_found)\n  :could_not_save -\u003e\n    response(:internal_server_error)\nend\n```\n\n## OK Pipe\n\nThe pipe (`~\u003e\u003e`) is equivalent to `bind`/`flat_map`.\nThe pipe (`~\u003e`) is equivalent to `map`.\n\nThese macros allows pipelining result tuples through multiple functions\nfor an extremely concise happy path.\n\n```elixir\nuse OK.Pipe\n\ndef get_employee_data(file, name) do\n  {:ok, file}\n  ~\u003e\u003e File.read\n  ~\u003e String.upcase\nend\n```\n\nUse `~\u003e\u003e` for `File.read` because it returns a result tuple.\nUse `~\u003e` for `String.upcase` because it returns a bare value that should be wrapped in an ok tuple.\n\n## Semantic matches\n\n`OK` provides macros for matching on success and failure cases.\nThis allows for code to check if a result returned from a function was a\nsuccess or failure while hiding implementation details about how that result is\nstructured.\n\n```elixir\nimport OK, only: [success: 1, failure: 1]\n\ncase fetch_user(id) do\n  success(user) -\u003e\n    user\n  failure(:not_found) -\u003e\n    create_guest_user()\nend\n```\n\n## FAQ\n\n#### Why does `OK` not catch raised errors?\n\nFor the main rational behind this decision see the article [Errors are not exceptional](http://crowdhailer.me/2018-08-26/errors-are-not-exceptional/)\n\nTwo other reasons:\n- Exceptional input and errors are not the same thing,\n  `OK` leaves raising exceptions as a way to handle errors that should never happen.\n- Calls inside try/1 are not tail recursive since the VM needs to keep the stacktrace in case an exception happens.\n  [see source](https://github.com/elixir-lang/elixir/blob/22bd10a8170af0b187029d115abe4cc8edcf2ae6/lib/elixir/lib/kernel/special_forms.ex#L1622).\n\n#### What about other shapes of error and success?\n\n- Accepting any extra forms is a slippery slope, and they are not always unambiguous.\n  If a library is not returning errors as you like it is very easy to wrap in a custom function.\n\n  ```elixir\n  def fetch_foo(map) do\n    case Map.fetch(map, :foo) do\n      {:ok, foo} -\u003e {:ok, foo}\n      :error -\u003e {:error, :no_foo}\n    end\n  end\n  ```\n\n#### What changed in version 2.0\n\n- `OK.with` was deprecated.\n- `use OK.Pipe` was added.\n- `OK.bind` was renamed `OK.flat_map`.\n\n## Additional External Links and Resources\n\n- Elixir Forum\n  - [OK v1 library](https://elixirforum.com/t/ok-elegant-error-handling-for-elixir-pipelines-version-1-0-released/1932/)\n- [Railway programming](http://www.zohaib.me/railway-programming-pattern-in-elixir/)\n- Similar Libraries\n  - [exceptional](https://github.com/expede/exceptional)\n  - [elixir-monad](https://github.com/nickmeharry/elixir-monad)\n  - [happy_with](https://github.com/vic/happy_with)\n  - [monad](https://github.com/rmies/monad)\n  - [ok_jose](https://github.com/vic/ok_jose)\n  - [towel](https://github.com/knrz/towel)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCrowdHailer%2FOK","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCrowdHailer%2FOK","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCrowdHailer%2FOK/lists"}