Customizing error reports

Add diagnostic data to every error report, or conditionally adjust error reports.

Before-notify callbacks

If you’d like to add diagnostic data to every error report, or adjust error reports conditionally, you can use a before-notify callback, which will be run before each error report is sent:

Bugsnag.beforeNotify(new BeforeNotify() {
    @Override
    public boolean run(Error error) {
        // Attach customer information to every error report
        error.addToTab("account", "name", "Acme Co.");
        error.addToTab("account", "paying_customer", true);

        // Return `false` if you'd like to stop this error being reported
        return true;
    }
});

The callback gives you access to the error object, so you can inspect and modify the error report which is about to be sent to Bugsnag.

You can also return false from your callback to stop this error report being reported.

The error object

The following properties and methods are available on the Error object.

addToTab

Sets a piece of information to be displayed in the Bugsnag.com error page. The first argument is the tab name, the second argument is the key for the data, and the third argument is used as the value.

error.addToTab("user", "role", "Administrator");

getContext

Returns the context of the error. The context is a summary what what was occurring in the application at the time of the crash, if available, such as the visible activity.

getException

Returns the thrown Throwable.

getExceptionName

Returns the full name of the Throwable.

getMetaData

Returns the attached exception metadata, which can be modified to add or remove fields.

MetaData metadata = error.getMetaData();
metadata.clearTab("user");

getStackTrace

Returns the Throwable’s array of StackTraceElements.

getUser

Returns the User that has been associated with the Error report.

setContext

Sets the context of the error. The context is a summary what what was occurring in the application at the time of the crash, if available, such as the visible activity.

error.setContext("Pause menu");

setGroupingHash

Sets the groupingHash used by Bugsnag.com to manually override the default grouping technique. This option is not recommended, and should be used carefully when used.

Any errors that are sent to Bugsnag, that have the same groupingHash will be grouped as one. As the name implies, this option accepts a hash of sorts.

// ... generate the hash
String groupingHash = "f8803769f3e293dfcabdb6dec5100b8c52c6ae6b";
error.setGroupingHash(groupingHash);

setSeverity

Overrides the severity of the error. Valid severities are error, warning and info.

error.setSeverity("warning");

setUser

Sets the user associated with the error report.

error.setUser("123", "user@example.com", "Bob Johnson");

Modifying the stack trace

It is possible to modify the stack trace that is associated with the error report in the following way:

Create a recursive function which loops copies each stack trace element and sets it back to the Throwable object. Any modifications to the stack trace can be done within this loop

private void alterStacktrace(Throwable t) {
    StackTraceElement[] trace = t.getStackTrace();
    List<StackTraceElement> newTrace = new ArrayList<>();

    for (StackTraceElement frame : trace) {
        // YOUR CODE HERE
        newTrace.add(new StackTraceElement(
                frame.getClassName(),
                frame.getMethodName(),
                frame.getFileName(),
                frame.getLineNumber()));
    }

    t.setStackTrace(newTrace.toArray(new StackTraceElement[newTrace.size()]));

    if (t.getCause() != null) {
        alterStacktrace(t.getCause());
    }
}

Then call this new function from the BeforeNotify function

alterStacktrace(error.getException());