In order to quickly reproduce and fix errors, it is often helpful to send additional application-specific diagnostic data to Bugsnag.
If you’d like to add diagnostic data to every error report, or adjust error reports conditionally, you can add callbacks which will be invoked each time a report will be sent:
class ApplicationController < ActionController::Base
before_bugsnag_notify :add_diagnostics_to_bugsnag
# Your controller code here
private
def add_diagnostics_to_bugsnag(report)
report.add_tab(:diagnostics, {
product: current_product.name
})
end
end
This callback will be executed for every handled and unhandled exception detected within your Rails app.
See the report object for methods available inside the callback.
If you are using custom error classes within your application, diagnostic data can be automatically attached to each error report within the exception class itself.
This is achieved by creating a bugsnag_meta_data
function on the custom error class that returns a hash
with the data you wish to attach.
class MyCustomError < StandardError
attr_reader :metadata
def initialize(message, metadata)
super(message)
@metadata = metadata
end
def bugsnag_meta_data
{tabname: metadata}
end
end
Bugsnag.notify(MyCustomError.new("Error message", value1: '1', value2: {nested: 3}))
This will add a tab with the name tabname
on the dashboard with the corresponding data listed in it.
add_tab
Call add_tab on a report object to add a tab to the error report so that it would appear on your dashboard.
report.add_tab(:user_info, { name: current_user.name })
The first parameter is the tab name that will appear in the error report and the second is the key, value list that will be displayed in the tab.
api_key
Set the project API key for the error report. The API key is normally set in the configuration, but it can be overridden to report to a different API key in some situations.
report.api_key = 'your-api-key-here'
context
Set the context of the error report. This is notionally the location of the error and should be populated automatically. Context is displayed in the dashboard prominently.
report.context = 'billing'
exceptions
Allows you to read the exceptions that will be combined into the report.
puts report.exceptions.first.message + ' found!'
grouping_hash
Sets the grouping hash of the error report. All errors with the same grouping hash are grouped together. This is an advanced usage of the library and mis-using it will cause your errors not to group properly in your dashboard.
report.grouping_hash = exception.message + exception.class.to_s
ignore!
Calling ignore!
on a report object will cause the report to not be sent to Bugsnag. This means that you can choose dynamically not to send an error depending on application state or the error itself.
report.ignore! if foo == 'bar'
meta_data
Provides access to the meta_data in the error report.
report.ignore! if report.meta_data[:sidekiq][:retry_count] < 2
remove_tab
Removes a tab completely from the error report
report.remove_tab(:request)
severity
Set the severity of the error. Severity can be error
, warning
or info
.
report.severity = 'error'
user
You can set or read the user with the user property of the report.
report.user = {
id: current_user.id,
email: current_user.email,
name: current_user.name
}