Does Phoenix have something similar to Rails’s rescue_from?
The action_fallback feature in Phoenix 1.3 combined with with blocks reduces the boilerplate considerably.
action_fallback MyApp.Web.FallbackController
def create(conn, params) do
with {:ok, cmd = %CreateUserCommand{}} <- build_create_user_command(params), # cast params to embedded ecto schema
{:ok, user = %User{}} <- create_user(cmd),
{:ok, job_id} <- send_welcome_email_async(user) do
render(conn, "show.json", user: user)
end
end
As long as the FallbackController has…
the v1.3 is still in a rc phase, so not officially released, but it seems to work really well already (at least for me). More in this article:
http://swanros.com/phoenix-1-3-is-pure-love-for-api-development/
You can override action on your controllers no problem, just call super() wrapped in a try to handle errors specially. I do that in my big project in quite a few areas via a set of helpers.