I'm using a library in my application which regularly returns tuples of {:ok, value}
or {:error, error}
. In the cases where we assume there may be an error (such as a user input), we're handling those cases explicitly. For all other cases, I would prefer to assume they will be properly handled and if not, have the failure case be handled with a redirect.
I started trying to solve this issue by using a function:
def show(conn, %{"id" => id}) do user = client.user_show(conn.assigns.endpoint, id) |> get_value(conn) # rest of method omittedenddef get_value({:ok, value}, _conn), do: valuedef get_value({:error, value}, conn) do conn |> put_flash(:error, "Client error") |> redirect(to: "/") |> haltend
This seemed like a good idea, but unfortunately, the conn was still continuing, despite the halt. This makes sense because the code path didn't split.
So it seems like the better solution would be to implement this function to raise a specific error and then try to resolve it globally.
Unfortunately, I can't find a way to handle an error at the global level with Phoenix. The only possible option I see is using the ErrorView
, but I don't want just to show an error, I want to redirect appropriately depending on the error.
Am I missing something or is this not possible currently in Phoenix?