Skip to content

Instantly share code, notes, and snippets.

@dennyabrain
Created April 30, 2024 04:48
Show Gist options
  • Save dennyabrain/35434b209bb91562fb80f0d3b82475ed to your computer and use it in GitHub Desktop.
Save dennyabrain/35434b209bb91562fb80f0d3b82475ed to your computer and use it in GitHub Desktop.
Elixir Convention for Error Handling with decent User Experience
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
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