Rails: Rescuing exceptions for specific exception types

By default most exceptions in Rails will render a 500 error page and will create a new issue in your error monitoring. There are some built-in rules Show archive.org snapshot in Rails that

  • render a different error than 500
  • will rescue the exception and not create an issue in your error monitoring

A good example is ActiveRecord::NotFound: You don't want an exception in your error monitoring when users navigate to e.g. a blog post that does not exist anymore. And it makes sense to show the user and the crawler a 404 error instead of a 500 error.

Configuring specific exceptions to rescue responses

In case you need to rescue more responses, you can add them to your config/application.rb. Here is an example for an exception from consul Show archive.org snapshot , that raises each time a user tries to access a page she has no access to.

config.action_dispatch.rescue_responses.merge!(
  { 'Consul::Powerless' => :not_found },
)
Emanuel 11 months ago