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 events, or adjust event data conditionally, you can use a callback. To get started, you can register a function to be used as the before_notify
callback:
def callback(event):
# You can set properties of the event and
# add your own custom metadata.
event.user = {"id": current_user.id,
"name": current_user.name,
"email": current_user.email}
event.add_tab("account", {"paying": current_user.acccount.is_paying()})
# Call `callback` before every event
bugsnag.before_notify(callback)
The callback gives you access to the Event object, so you can inspect and modify the error event which is about to be sent to BugSnag.
If you want to prevent an event from being sent to BugSnag, you can return False
from within a before_notify
callback.
def callback(event):
# Discard by error class
if isinstance(event.original_error, KeyboardInterrupt):
return False
# Custom behavior can be added based on the request context
if event.request is not None and event.request.status_code == 505:
return False
# Don't send `SystemExit` errors with an exit code of 0
if isinstance(event.original_error, SystemExit) and event.original_error.code == 0:
return False
# Call `callback` before every event
bugsnag.before_notify(callback)
Event
objectThe event object can be altered using any of the following fields as well as the keyword arguments in Notification options.
add_feature_flag
Declare a single feature flag or experiment with variant as an optional second parameter.
event.add_feature_flag("Checkout button color", "Blue")
event.add_feature_flag("New checkout flow")
For more information, see Feature flags & experiments.
add_feature_flags
Declare multiple feature flags or experiments.
event.add_feature_flags(
[
bugsnag.FeatureFlag("Checkout button color", "Blue"),
bugsnag.FeatureFlag("New checkout flow")
]
)
For more information, see Feature flags & experiments.
clear_feature_flag
Remove a single feature flag or experiment.
event.clear_feature_flag("Checkout button color")
clear_feature_flags
Remove all feature flags and experiments.
event.clear_feature_flags()
errors
event.errors
is a list of one or more Error
objects. The first item in the list represents the raised exception. Each subsequent item represents the exception that caused the preceding one.
A reference to the actual exception object that caused the event is available through event.original_error
.
An Error
object contains the following information:
property | type | description |
---|---|---|
error_class |
str |
The fully-qualified class name of the error |
error_message |
str |
The string representation of the error |
stacktrace |
List[Dict] |
A representation of the stacktrace |
type |
str |
The type of error based on the originating platform. This will be python unless changed manually |
Each dictionary in the stacktrace
contains the following information:
key | type | description |
---|---|---|
file |
str |
The location of the source file |
lineNumber |
int |
The line number within the source file this stackframe refers to |
method |
str |
The name of the method that was being executed |
inProject |
bool |
Whether the frame occurred inside the project codebase. This depends on the configured project_root |
code |
Optional[Dict] |
A dictionary of code lines surrounding the called method, if send_code is enabled |
exception
The original exception used to generate the event. The name and string value of the exception is used as the error class and message of the first element in the errors
list. The exception itself is not serialized as a part of the event metadata.
Deprecated: Use original_error
instead.
original_error
The original exception used to generate the event. The name and string value of the exception is used as the error class and message of the first element in the errors
list. The exception itself is not serialized as a part of the event metadata.
request
The HTTP request being processed at the time of the event, if any. The request headers, parameters, and HTTP method are used to generate the request metadata attached to the event. The request itself is not serialized as a part of the event metadata.