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 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

There are two ways to add context to errors in your code: global and local. While you can add any key/value data to context, a few keys have a special meaning in Honeybadger.

Global context

honeybadger.set_context allows you to send additional information to the Honeybadger API to assist in debugging. This method sets global context data and is additive - every time you call it, it adds to the existing set unless you call reset_context, documented below.

python
from honeybadger import honeybadger honeybadger.set_context(my_data='my_value')

Context data is thread-local, meaning each thread maintains its own separate context.

honeybadger.reset_context clears the global context dictionary.

python
from honeybadger import honeybadger honeybadger.reset_context()

Local context

What if you don't want to set global context data? You can use Python context managers to set case-specific contextual information.

python
# from a Django view from honeybadger import honeybadger def my_view(request): with honeybadger.context(user_email=request.POST.get('user_email', None)): form = UserForm(request.POST) # ...

Special context keys

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

Option Description
_tags Any value passed in the _tags key will be processed as tags for the error.
user_id The 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.

Automatic context

When using Django, the middleware automatically adds user information to error context:

  • user_id - from request.user.id
  • username - from request.user.get_username()

This happens automatically for authenticated users when an error occurs.

Limits

Honeybadger's servers enforce 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 on the server, and the notification will still be processed.