Adding Context to Errors

Sometimes, default exception data just isn't enough. If you have extra data that will help you in debugging, send it as part of an error's context. 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 exception
  • You need to send raw POST data for use in debugging
  • You have any other metadata you'd like to send with an exception

Honeybadger supports two types of context: global and local.

Global Context

Global context is automatically reported with any exception which occurs after the context has been created:

Honeybadger.context({
  my_data: 'my value'
})

A few other methods are also available when working with context:

# Clear the global context:
Honeybadger.context.clear!

# Fetch the global context:
Honeybadger.get_context

Context in Honeybadger.notify

You can also add context to a manual error report using the :context option, like this:

Honeybadger.notify(exception, context: {
  my_data: 'my local value'
})

Local context always overrides any global values when the error is reported.

Special Context Values

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

Option Description
:user_id The String user ID used by Honeybadger to aggregate user data across occurrences on the error page.
:user_email Same as :user_id, but for email addresses
:tags A String comma-separated list of tags. When present, tags will be applied to errors with this context.