Customizing error reports

The OnBeforeNotify configuration option allows for modifying reported information by editing fields on bugsnag.Event.

Modifying report information

Any value on an event can be overridden prior to delivery:

bugsnag.OnBeforeNotify(
    func(event *bugsnag.Event, config *bugsnag.Configuration) error {

        // Change the endpoint context of an event
        event.Context = "GET /api/cart@handler"
        return nil
    })

Or removed altogether:

bugsnag.OnBeforeNotify(
    func(event *bugsnag.Event, config *bugsnag.Configuration) error {

        // Remove a potentially sensitive parameter from the raw payload data
        delete(event.MetaData["params"], "credit_card_param")
        return nil
    })

Overriding BugSnag’s grouping algorithm

If you need to override BugSnag’s grouping algorithm, you can set the GroupingHash in an OnBeforeNotify:

bugsnag.OnBeforeNotify(
    func (event *bugsnag.Event, config *bugsnag.Configuration) error {

        // Group events using custom grouping hash (String)
        event.GroupingHash = calculateGroupingHash(event)
        return nil
    })

Discarding errors

If you want to prevent an event from being sent to BugSnag, you can return an error in an OnBeforeNotify callback, for example:

bugsnag.OnBeforeNotify(
    func(event *bugsnag.Event, config *bugsnag.Configuration) error {

        // Discard an event by returning an error
        return errors.New("Discarding Error")
    })

Creating multiple clients

Separate clients can be created to report handled errors with independent configuration options.

client := bugsnag.New(bugsnag.Configuration{
    APIKey: "YOUR_OTHER_API_KEY",
})

client.Notify(err)

Any place that lets you pass in rawData also supports custom configuration. For example, to send an individual error to a different project:

bugsnag.Notify(err, bugsnag.Configuration{APIKey: "YOUR_OTHER_API_KEY"})

The bugsnag.Event object

Context

A short description of the error location, such as the URL path or top filename from the stack trace.

Note: This is not a context.Context. See bugsnag-context for more details.

event.Context = "billing"

Error

The original error which caused the event.

ErrorClass

The name or title of the report sent to BugSnag

event.ErrorClass = "failed retry"

GroupingHash

A hint for how a report should be grouped with other events in a project. All reports with the same grouping hash will be grouped together.

// group events by error class and file
event.GroupingHash = fmt.Sprintf("%s:%s", event.Stacktrace[0].File, event.ErrorClass)

Message

The description or summary of the report sent to BugSnag

event.Message = "Invalid credential format"

MetaData

Additional information appended to the report, organized as key/value pairs in named sections.

event.MetaData.Add("job", "id", job.Id)

RawData

The raw information available about the event. This structure is not appended to the report directly but is available to cache request information or additional context.

for _, datum := range event.RawData {
    if user, ok := datum.(*MyUserInfo); ok {
        event.User = &User{Id: user.Id}
    }
}

Stacktrace

An array of stack frames leading to the original error.

// trim logger from stack trace
if event.Stacktrace[0].File == "mylogger.go" {
    event.Stacktrace = event.Stacktrace[1:]
}

Unhandled

By default this is true if the event was automatically detected by BugSnag and false if it was reported manually via Bugsnag.notify. See our product pages for more information on handled vs unhandled events.

event.Unhandled = true

Changing the Unhandled flag for an event will affect how it contributes to your application’s stability score.

User

event.User = &User{Id: myUser.Id, Email: myUser.EmailAddress}

Information about the user who encountered the event

Request

Information about the HTTP request for which the event occurred

if event.Request.URL == "/dev-check" {
    // ignore or modify events from a particular endpoint
}