Method: Honeybadger::Agent#context

Defined in:
lib/honeybadger/agent.rb

#context(context = nil, &block) ⇒ Object, self

Save global context for the current request.

Examples:

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

# Inside a Rails controller:
before_action do
  Honeybadger.context({user_id: current_user.id})
end

# Explicit conversion
class User < ActiveRecord::Base
  def to_honeybadger_context
    { user_id: id, user_email: email }
  end
end

user = User.first
Honeybadger.context(user)

# Clearing global context:
Honeybadger.context.clear!

Parameters:

  • context (Hash) (defaults to: nil)

    A Hash of data which will be sent to Honeybadger when an error occurs. If the object responds to #to_honeybadger_context, the return value of that method will be used (explicit conversion). Can include any key/value, but a few keys have a special meaning in Honeybadger.

Options Hash (context):

  • :user_id (String)

    The user ID used by Honeybadger to aggregate user data across occurrences on the error page (optional).

  • :user_email (String)

    The user email address (optional).

  • :tags (String)

    The comma-separated list of tags. When present, tags will be applied to errors with this context (optional).

Returns:

  • (Object, self)

    value of the block if passed, otherwise self

[View source]

272
273
274
275
276
277
# File 'lib/honeybadger/agent.rb', line 272

def context(context = nil, &block)
  block_result = context_manager.set_context(context, &block) unless context.nil?
  return block_result if block_given?

  self
end