Read more

Rails: Rescuing exceptions for specific exception types

Emanuel
May 08, 2023Software engineer at makandra GmbH

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
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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 },
)
Posted by Emanuel to makandra dev (2023-05-08 09:00)