Skip to content
Andreas Dausenau edited this page Aug 12, 2022 · 1 revision

Errors

Ez-on-rails provides a default error handling strategy for both, the administration backend ad the API. Have a look at the following sections to see how to use it.

API

The API catches exception inheriting from the EzOnRails::ApiError class. By raising some error that inherits from this class, the execution of the action will be stopped and an error json will be returned.

Throwing API Errors

By just throwing an error inheriting from EzOnRails::ApiError the error will be automaticly rendered.

raise EzOnRails::ForbiddenError unless current_user.super_admin?

The following errors are available out of the box, some of them need some passed arguments for additional information to render the error json:

  • EzOnRails::ForbiddenError - renders default 403 error
  • EzOnRails::InternalServerError - renders default 500 error
  • EzOnRails::InvalidRequestTypeError - renders default 406 error, is automatically thrown if a non json request was sent to the api
  • EzOnRails::InvalidVersionError - renders default 410 error, is automatically thrown if the request has a non matching api version header
  • EzOnRails::ResourceNotFoundError.new message: 'user not found' - renders a 404 error, expects a named message parameter to be able to describe what resource was not found
  • EzOnRails::UnauthorizedError - renders a default 401 error, is automatically thrown if the user accesses some restricted area without being logged in
  • EzOnRails::ValidationFailedError.new record - renders a 422 error, expects the invalid record having active record validation errors as parameter

If you want to create own errors, have a look at the following section.

Creating own API Errors

You can create errors wherever you want. You only need to inherit from EzOnRails::ApiError. The superclass expects the two named parameters message and http_status. The message is the shown message in the json error. The http_status is the status code that is returned.

class CustomForbiddenError < EzOnRails::ApiError
  def initialize
    super(message: 'Nah, don't try that again please.', http_status: :forbidden)
  end
end

Administration Backend

The administration backend does not provide global exception handling, but it provides views you can use to render the errors.

Rendering error page

The following example renders an 403 forbidden error.

    render 'ez_on_rails/errors/403', status: :forbidden

The following error pages are available out of the box:

  • 401 - unauthorized
  • 403 - forbidden
  • 404 - not found
  • 500 - unknown

If you want to add additional error pages or just create a new one to customize the behavior, have a look at the following section.

Creating own error page

You can create a new error page wherever you want. But to get the default error page style ez-on-rails already provides with the errors mentioned above, you can render the partial ez_on_rails/errors/default_error_pag. This partial just takes the error_text as argument. The following example shows a simple error page using the partial in slim html.

= render partial: 'ez_on_rails/errors/default_error_page', locals: {\
  error_text: t(:'ez_on_rails.internal_server_error')\
}