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 set a callback which will be run before each error report is sent:

$bugsnagWordpress->setBeforeNotifyFunction(function ($report) {
    // For example, add customer information to every error report
    $report->setMetaData([
        'account' => [
            'name' => 'Acme Co.',
            'paying_customer' => 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 delivered.

The $error object

The following properties and methods are available on the $error object.

setName

Set the error name sent in the error report

$error->setName('OopsError');

setMessage

Set the error message sent in the error report

$error->setMessage('This was a mistake.');

setSeverity

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

By default, all unhandled exceptions are set to severity error and all handled exceptions are set to severity warning.

$error->setSeverity('info');

setGroupingHash

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.

$error->setGroupingHash($exception->message . $exception->class);

setMetaData

Set additional metadata to send with this error report, or filter existing metadata.

$error->setMetaData([
    'account' => array(
        'paying' => true,
        'name' => 'Acme Co'
    )
]);

You can also pass a 2nd paramater of false stop us merging your metadata, and force us to completely replace it.

setUser

You can set the associated user on the error.

$error->setUser([
    'id' => '123456',
    'name' => 'Leeroy Jenkins',
    'email' => 'leeeeroy@jenkins.com',
]);

stacktrace

Inspect or modify the stacktrace of the error report.

For example, if Bugsnag is called by a wrapper library, you can remove stack frames from the stacktrace:

function before_bugsnag_notify($error) {
    // Extract the first frame of the stacktrace
    $firstFrame = $error->stacktrace->frames[0];

    // Check if the first frame is in a particular file
    if ($firstFrame && $firstFrame['file'] === 'My_Logger.php') {
        // Remove the first frame from the stacktrace
        array_splice($error->stacktrace->frames, 0, 1);
    }
}