tokumei v0.6.4 Tokumei.ErrorHandler

Handle failures from request handlers.

Extension to the Raxx interface to provide better error handling. Tokumei.ErrorHandler modifies all responses of the form {:error, reason}. It will first match the reason against the appropriate failure handler. If no failure handler matches it will return a default 500 response.

Using Tokumei.ErrorHandler will alias all Tokumei Exceptions.

defmodule MyApp.Router do
  use Tokumei.NotFound
  use Tokumei.Router
  use Tokumei.ErrorHandler

  import Raxx.Response

  route "/checkout" do
    post(request) ->

      # Actions can return an exception as the reason for a error tuple
      {:error, :subscription_expired}
  end

  # Any error can be handled using an error block
  error :subscription_expired do

    # payment_required/1 returns a Raxx.Response with status 402
    payment_required("Please pay up, :-)")
  end

  # Routing errors can also be intercepted
  error %NotFoundError{path: path} do
    path = "/" <> Enum.join(path, "/")

    # For example, to send a custom response message.
    not_found("Could not find #{path}")
  end
end

Summary

Functions

error(exception, list) (macro)