Rails provides a common interface, ActiveSupport::ErrorReporter,
for error reporting services.
his allows external gems such as HoneyBadger, Sentry, Rollbar and more to standardize their
monitoring of errors throughout the platform.
The ActiveSupport::ErrorReporter follows a pub-sub pattern,
where subscribers can register to receive error reports.
Every subscriber must be registered
and respond to,
The context hash is used to provide additional information about the error.
For example, the context hash can include the current user, the current request,
the current controller,
Here’s an example,
To capture an error ActiveSupport::ErrorReporter provides two methods, handle and record.
The basic difference is that handle swallows the error while record re-raises the error.
Both methods take an exception
and an optional context hash.
All captured errors are sent to all registered subscribers.
In order to capture the source of the error,
users previously could send the source as part of the context hash.
This was not ideal because it was not standardized
and hence was not possible to filter on the source.
Thanks to this PR,
Rails now provides a standardized way to capture the source of the error.
The source can be set using the source attribute of the ErrorReporter.
This works in a similar fashion to the severity attribute.
The source can be set to any string value.
The default value is application.
Let’s now see an example. First let’s modify the Subscriber to capture the source and severity attribute.
We then register the subscriber
and then capture an error with a source.
Now, we can further modify the error subscriber
to ignore all “application” source errors.
This makes it easier to filter out internal errors
and focus on specific errors.