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.

Defining Context On Objects

Context must either be a Hash, or it must define the method #to_honeybadger_context to return a Hash. For example, to pass a User instance to Honeybadger.context:

class User < ApplicationRecord
  def to_honeybadger_context
    { user_id: id, user_email: email }
  end
end

user = User.last

Honeybadger.context(user)

When the #to_honeybadger_context method is defined on an Exception class, the context will be automatically added when the exception is reported:

class CustomError < StandardError
  def to_honeybadger_context
    { tags: 'custom' }
  end
end

raise CustomError, 'This error will be reported with context'

Limits

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