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.context/1
is provided for adding extra data to the notification
that gets sent to Honeybadger. You can make use of this in places such as a Plug
in your Phoenix Router or Controller to ensure useful debugging data is sent along.
def MyPhoenixApp.Controller
use MyPhoenixApp.Web, :controller
plug :set_honeybadger_context
def set_honeybadger_context(conn, _opts) do
user = get_user(conn)
Honeybadger.context(user_id: user.id, account: user.account.name)
conn
end
end
Honeybadger.context/1
stores the context data in the process dictionary, so
it will be sent with errors/notifications on the same process. The following
Honeybadger.notify/1
call will not see the context data set in the previous line.
Honeybadger.context(user_id: 5)
Task.start(fn ->
# this notify does not see the context set earlier
# as this runs in a different elixir/erlang process.
Honeybadger.notify(%RuntimeError{message: "critical error"})
end)
Local Context
You can also add context to a manual error report using the metadata
option,
like this:
context = %{user_id: 5, account_name: "Foo"}
Honeybadger.notify(exception, metadata: context, stacktrace: __STACKTRACE__)
Local context always overrides any global values when the error is reported.
Special Context Keys
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 |
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
When an error notification includes context data that exceed these limits, the context data will be truncated, and the notification will still be processed.