Skip to content
🚫 Error response handlers for a Rails controller.
Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.


Build Status: master Gem Version MIT license

Error response handlers for a Rails controller.


Add this line to your application's Gemfile:

gem 'erratum_responsum'

And then execute:

$ bundle

Or install it yourself as:

$ gem install erratum_responsum


Add the concern to your ApplicationController or ApiController

require 'erratum_responsum'

class ApiController < ActionController::API
  include ErratumResponsum

Media Type

You can optionally set the class variable error_media_type to change the Content-Type of error responses.

  self.error_media_type = 'application/vnd.xpbytes.errors.v1+json'

Rescue Errors

In order to use the error responses, use rescue_from to handle them:

  rescue_from CanCan::AccessDenied, AuthorizedTransaction::TransactionUnauthorized, with: :forbidden
  rescue_from OptimisticallyStale::MissingLockVersion, ActionController::BadRequest,
              RequestMissingParam, with: :bad_request
  rescue_from ActiveRecord::RecordNotFound, with: :not_found
  rescue_from ActionController::UnknownFormat, NoAcceptSerializer, with: :not_acceptable
  rescue_from ResourceGone, with: :gone
  rescue_from ActiveRecord::StatementInvalid, ActiveRecord::RecordInvalid,
              ContentDoesNotMatchContentType, with: :unprocessable_entity
  rescue_from ActiveRecord::RecordNotUnique, ActiveRecord::StaleObjectError, with: :conflict
  rescue_from ContentTypeNotRecognised, with: :unsupported_media_type

Now, when one of these error is raised in a controller action, the error response is generated automatically.

When the exception has more information, such as an error_code, the code will use that instead, prefixed with Ex. If there is no such information, the error class name is used to generate the error code, prefixed with Gx. Change this behaviour by overriding def error_code(error).


After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at XPBytes/erratum_responsum.

You can’t perform that action at this time.