Skip to content

Adding context to errors

Honeybadger can display additional custom key/value metadata — or “context” — with each error report. Context is what you’re looking for if:

  • You want to record the current user’s id or email address at the time of an error
  • You need to send additional debugging information with an error
  • You have any other metadata you’d like to send with an error

There are two ways to add context to errors in your code: global and local.

Use honeybadger.SetContext to set context data that will be sent with any error that occurs:

honeybadger.SetContext(honeybadger.Context{
"user_id": 1,
})

For example, it’s often useful to record the current user’s ID when an error occurs in a web app. To do that, use SetContext to set the user id on each request. If an error occurs, the id will be reported with it.

Note: This method is currently shared across goroutines, and therefore may not be optimal for use in highly concurrent use cases, such as HTTP requests. See issue #35.

To clear all context data that was previously set with SetContext:

honeybadger.ClearContext()

You can also add context to a single error report using Context as an optional argument to honeybadger.Notify:

honeybadger.Notify(err, honeybadger.Context{"user_id": 2})

Local context is useful when you want to add context that’s specific to a particular error, without affecting global context.

While you can add any key/value data to context, a few keys have special meaning in Honeybadger:

KeyDescription
user_idThe String user ID used by Honeybadger to aggregate user data across occurrences on the error page.
user_emailSame as user_id, but for email addresses

Honeybadger uses the following limits to ensure the service operates smoothly for everyone:

  • Nested objects have a max depth of 20
  • Context values have a max size of 64Kb

When an error notification includes context data that exceed these limits, the context data will be truncated, and the notification will still be processed.