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 a clause that can match the {:error, %Changeset{}}
or {:error, reason}
tuple from one of the failing steps then you’ll get a 4xx range response.