Customizing error reports

In order to quickly reproduce and fix errors, it is often helpful to send additional application-specific diagnostic data to Bugsnag.

Updating events using callbacks

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.

Discarding events

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)

The Event object

The event object can be altered using any of the following fields as well as the keyword arguments in Notification options.

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.