Created
April 30, 2024 04:48
-
-
Save dennyabrain/35434b209bb91562fb80f0d3b82475ed to your computer and use it in GitHub Desktop.
Elixir Convention for Error Handling with decent User Experience
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Application do | |
def get_queue_url() do | |
url = Application.get_env(:dau, RabbitMQ) |> Keyword.get(:urls) | |
case url do | |
url -> {:ok, url} | |
nil -> {:error, "Queue URL is not set"} | |
end | |
rescue | |
_ -> {:error, "Unexpected Error : Fetching Queue URL"} | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Job do | |
def add(attrs) do | |
with {:ok, job} <- JobRequest.new(attrs), | |
{:ok, url} <- Application.get_queue_url(), | |
{:ok, conn} <- Connection.open(url), | |
{:ok, channel} <- Channel.open(conn), | |
{:ok, response} <- Queue.add(channel, job) do | |
{:ok, response} | |
else | |
{:error, reason} -> {:error, "Unable to add job : #{inspect(reason)}"} | |
end | |
rescue | |
_ -> {:error, "Malformed Request"} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment