Quantcast
Viewing all articles
Browse latest Browse all 2

Answer by tkowal for How do I handle custom errors, globally, in Phoenix?

The halt function doesn't actually provide any branching logic. It only sets conns private field halted to true. This makes sure that other plugs will not process it further, but rest of the action will process it and override the halted field.

The solution is to move this function to a plug.

def client_error_plug(conn) do  user = client.user_show(conn.assigns.endpoint, id) |> get_value(conn)  case user do    {:ok, user} ->      assign(conn, :current_user, user)    {:error, reason} ->       conn      |> put_flash(:error, "Client error")      |> redirect(to: "/")      |> halt  endend

and in your controller just put"

plug :client_error_plug when action in [:show, ...]

It probably makes sense to give this plug a more meaningful name, but it depends on the application logic :) Now you can be sure that you have the user in assigns, because if it is not there the redirect will kick in.


Viewing all articles
Browse latest Browse all 2

Trending Articles