Method: Honeybadger::Agent#notify

Defined in:
lib/honeybadger/agent.rb

#notify(exception_or_opts = nil, opts = {}, **kwargs) ⇒ String, false

Sends an exception to Honeybadger. Does not report ignored exceptions by default.

Examples:

# With an exception:
begin
  fail 'oops'
rescue => exception
  Honeybadger.notify(exception, context: {
    my_data: 'value'
  }) # => '-1dfb92ae-9b01-42e9-9c13-31205b70744a'
end

# Custom notification:
Honeybadger.notify('Something went wrong.',
  error_class: 'MyClass',
  context: {my_data: 'value'}
) # => '06220c5a-b471-41e5-baeb-de247da45a56'

Parameters:

  • exception_or_opts (Exception, Hash, Object) (defaults to: nil)

    An Exception object, or a Hash of options which is used to build the notice. All other types of objects will be converted to a String and used as the :error_message.

  • opts (Hash) (defaults to: {})

    The options Hash when the first argument is an Exception.

  • kwargs (Hash)

    options as keyword args.

Options Hash (opts):

  • :error_message (String)

    The error message.

  • :error_class (String) — default: 'Notice'

    The class name of the error.

  • :backtrace (Array)

    The backtrace of the error (optional).

  • :fingerprint (String)

    The grouping fingerprint of the exception (optional).

  • :force (Boolean) — default: false

    Always report the exception when true, even when ignored (optional).

  • :sync (Boolean) — default: false

    Send data synchronously (skips the worker) (optional).

  • :tags (String)

    The comma-separated list of tags (optional).

  • :context (Hash)

    The context to associate with the exception (optional).

  • :controller (String)

    The controller name (such as a Rails controller) (optional).

  • :action (String)

    The action name (such as a Rails controller action) (optional).

  • :parameters (Hash)

    The HTTP request paramaters (optional).

  • :session (Hash)

    The HTTP request session (optional).

  • :url (String)

    The HTTP request URL (optional).

  • :cause (Exception)

    The cause for this error (optional).

Returns:

  • (String)

    UUID reference to the notice within Honeybadger.

  • (false)

    when ignored.

[View source]

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/honeybadger/agent.rb', line 127

def notify(exception_or_opts = nil, opts = {}, **kwargs)
  if !config[:'exceptions.enabled']
    debug { 'disabled feature=notices' }
    return false
  end

  opts = opts.dup
  opts.merge!(kwargs)

  if exception_or_opts.is_a?(Exception)
    already_reported_notice_id = exception_or_opts.instance_variable_get(:@__hb_notice_id)
    return already_reported_notice_id if already_reported_notice_id
    opts[:exception] = exception_or_opts
  elsif exception_or_opts.respond_to?(:to_hash)
    opts.merge!(exception_or_opts.to_hash)
  elsif !exception_or_opts.nil?
    opts[:error_message] = exception_or_opts.to_s
  end

  validate_notify_opts!(opts)

  add_breadcrumb(
    "Honeybadger Notice",
    metadata: opts,
    category: "notice"
  ) if config[:'breadcrumbs.enabled']

  opts[:rack_env] ||= context_manager.get_rack_env
  opts[:global_context] ||= context_manager.get_context
  opts[:breadcrumbs] ||= breadcrumbs.dup
  opts[:request_id] ||= context_manager.get_request_id

  notice = Notice.new(config, opts)

  config.before_notify_hooks.each do |hook|
    break if notice.halted?
    with_error_handling { hook.call(notice) }
  end

  unless notice.api_key =~ NOT_BLANK
    error { sprintf('Unable to send error report: API key is missing. id=%s', notice.id) }
    return false
  end

  if !opts[:force] && notice.ignore?
    debug { sprintf('ignore notice feature=notices id=%s', notice.id) }
    return false
  end

  if notice.halted?
    debug { 'halted notice feature=notices' }
    return false
  end

  info { sprintf('Reporting error id=%s', notice.id) }

  if opts[:sync] || config[:sync]
    send_now(notice)
  else
    push(notice)
  end

  if exception_or_opts.is_a?(Exception)
    exception_or_opts.instance_variable_set(:@__hb_notice_id, notice.id) unless exception_or_opts.frozen?
  end

  notice.id
end