Connection lost
Reconnecting. If you just saved something, check that it saved before trying again.
Server error
Reconnecting to the server. If you just saved something, check that it saved before trying again.
Gracefully handle 404s and other errors in Phoenix using Plug.ErrorHandler
0 comments
Instead of letting your Phoenix app crash with ugly errors, add this clean error handler:
#lib/myapp_web/router.ex
use Plug.ErrorHandler
@impl Plug.ErrorHandler
def handle_errors(conn, %{kind: _kind, reason: reason, stack: _stack}) do
has_message = Map.has_key?(reason, :message)
json(conn, %{
"error" => [%{
"code" => "generic",
"message" => if has_message, do: reason.message, else: "Problem occurred"
}]
})
end
Now your app returns a nice JSON response instead of raising errors, and the server wonβt crash. You can also return HTML with a generic 404 page πͺ
You can read more on hexdocs: https://hexdocs.pm/plug/Plug.ErrorHandler.html