11 November 2022

New error reporting in rails 7

While working through a bug where we were getting duplicate errors reported by sidekiq in honeybadger I learned about a new feature that was released in rails 7, but wasn’t documented. Have you ever written some code like this example below where you want to rescue an exception and still report to your error reporting service like honeybadger or sentry?

rescue SomethingIsBroken => error

Well a new feature was added in rails called Error Reporting, and now we have a simple consistent way of doing this in rails.

Rails.error.handle(SomethingIsBroken) do

The code above will handle the error if do_something raises an error and rails will automagically send it to the reporting service you are using in your app. To create a subscriber for the error reporting you just create an intializer like this. As of now honeybadger and sentry both create a subscriber for you so you can skip this if you use either of them.

# config/initializers/error_subscriber.rb
class ErrorSubscriber
  def report(error, handled:, severity:, context:, source: nil)
    MyErrorReportingService.report_error(error, context: context, handled: handled, level: severity)


Thats it, now you can use the consistent Rails.error.handle block when handling errors in your app and rails will make sure it gets sent to the subscribed error reporting service.

It will be in the docs for the next release of rails also, for now check out the rails error reporting docs in rails edge guides.

comments powered by Disqus