Class: Honeybadger::Notice

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Conversions
Defined in:
lib/honeybadger/notice.rb

Defined Under Namespace

Classes: Cause

Constant Summary collapse

TAG_SEPERATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The String character used to split tag strings.

/,|\s/.freeze
TAG_SANITIZER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The Regexp used to strip invalid characters from individual tags.

/\s/.freeze
PROJECT_ROOT_CACHE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Cache project path substitutions for backtrace lines.

{}
GEM_ROOT_CACHE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Cache gem path substitutions for backtrace lines.

{}
BACKTRACE_FILTERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

A list of backtrace filters to run all the time.

[
  lambda { |line|
    return line unless defined?(Gem)
    GEM_ROOT_CACHE[line] ||= Gem.path.reduce(line) do |line, path|
      line.sub(path, GEM_ROOT)
    end
  },
  lambda { |line, config|
    return line unless config
    c = (PROJECT_ROOT_CACHE[config[:root]] ||= {})
    return c[line] if c.has_key?(line)
    c[line] ||= if config.root_regexp
                  line.sub(config.root_regexp, PROJECT_ROOT)
                else
                  line
                end
  },
  lambda { |line| line.sub(RELATIVE_ROOT, STRING_EMPTY) },
  lambda { |line| line if line !~ %r{lib/honeybadger} }
].freeze

Constants included from Conversions

Conversions::MAX_CONTEXT_DEPTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Conversions

Context

Constructor Details

#initialize(config, opts = {}) ⇒ Notice

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Notice.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/honeybadger/notice.rb', line 190

def initialize(config, opts = {})
  @now   = Time.now.utc
  @pid   = Process.pid
  @id    = SecureRandom.uuid
  @stats = Util::Stats.all

  @opts = opts
  @config = config

  @rack_env = opts.fetch(:rack_env, nil)
  @request_sanitizer = Util::Sanitizer.new(filters: params_filters)

  @exception = unwrap_exception(opts[:exception])

  self.error_class = exception_attribute(:error_class, 'Notice') {|exception| exception.class.name }
  self.error_message = exception_attribute(:error_message, 'No message provided') do |exception|
    message = exception.respond_to?(:detailed_message) ?
      exception.detailed_message(highlight: false).sub(" (#{exception.class.name})", '') # Gems like error_highlight append the exception class name
      : exception.message
    "#{exception.class.name}: #{message}"
  end
  self.backtrace = exception_attribute(:backtrace, caller)
  self.cause = opts.key?(:cause) ? opts[:cause] : (exception_cause(@exception) || $!)

  self.context = construct_context_hash(opts, exception)
  self.local_variables = local_variables_from_exception(exception, config)
  self.api_key = opts[:api_key] || config[:api_key]
  self.tags = construct_tags(opts[:tags]) | construct_tags(context[:tags])

  self.url        = opts[:url]        || request_hash[:url]      || nil
  self.action     = opts[:action]     || request_hash[:action]   || nil
  self.component  = opts[:controller] || opts[:component]        || request_hash[:component] || nil
  self.params     = opts[:parameters] || opts[:params]           || request_hash[:params] || {}
  self.session    = opts[:session]    || request_hash[:session]  || {}
  self.cgi_data   = opts[:cgi_data]   || request_hash[:cgi_data] || {}
  self.details    = opts[:details]    || {}
  self.request_id = opts[:request_id] || nil

  self.session = opts[:session][:data] if opts[:session] && opts[:session][:data]

  self.breadcrumbs = opts[:breadcrumbs] || Breadcrumbs::Collector.new(config)

  # Fingerprint must be calculated last since callback operates on `self`.
  self.fingerprint = fingerprint_from_opts(opts)
end

Instance Attribute Details

#actionObject

The action (if any) that was called in this request.



124
125
126
# File 'lib/honeybadger/notice.rb', line 124

def action
  @action
end

#api_keyObject

The API key used to deliver this notice.



136
137
138
# File 'lib/honeybadger/notice.rb', line 136

def api_key
  @api_key
end

#backtraceObject

The backtrace from the given exception or hash.



91
92
93
# File 'lib/honeybadger/notice.rb', line 91

def backtrace
  @backtrace
end

Returns The collection of captured breadcrumbs.

Returns:



142
143
144
# File 'lib/honeybadger/notice.rb', line 142

def breadcrumbs
  @breadcrumbs
end

#causeObject

The exception cause if available.



81
82
83
# File 'lib/honeybadger/notice.rb', line 81

def cause
  @cause
end

#causesCause (readonly)

Returns A list of exception causes (see Cause).

Returns:

  • (Cause)

    A list of exception causes (see Cause)



88
89
90
# File 'lib/honeybadger/notice.rb', line 88

def causes
  @causes
end

#cgi_dataObject

CGI variables such as HTTP_METHOD.



112
113
114
# File 'lib/honeybadger/notice.rb', line 112

def cgi_data
  @cgi_data
end

#componentObject Also known as: controller

The component (if any) which was used in this request (usually the controller).



119
120
121
# File 'lib/honeybadger/notice.rb', line 119

def component
  @component
end

#contextObject

The context Hash.



109
110
111
# File 'lib/honeybadger/notice.rb', line 109

def context
  @context
end

#detailsObject

Custom details data



145
146
147
# File 'lib/honeybadger/notice.rb', line 145

def details
  @details
end

#error_classObject

The name of the class of error (example: RuntimeError).



103
104
105
# File 'lib/honeybadger/notice.rb', line 103

def error_class
  @error_class
end

#error_messageObject

The message from the exception, or a general description of the error.



106
107
108
# File 'lib/honeybadger/notice.rb', line 106

def error_message
  @error_message
end

#exceptionObject (readonly)

The exception that caused this notice, if any.



78
79
80
# File 'lib/honeybadger/notice.rb', line 78

def exception
  @exception
end

#fingerprintObject

Custom fingerprint for error, used to group similar errors together.



94
95
96
# File 'lib/honeybadger/notice.rb', line 94

def fingerprint
  @fingerprint
end

#idObject (readonly)

The unique ID of this notice which can be used to reference the error in Honeybadger.



75
76
77
# File 'lib/honeybadger/notice.rb', line 75

def id
  @id
end

#local_variablesObject

Local variables are extracted from first frame of backtrace.



133
134
135
# File 'lib/honeybadger/notice.rb', line 133

def local_variables
  @local_variables
end

#paramsObject Also known as: parameters

A hash of parameters from the query string or post body.



115
116
117
# File 'lib/honeybadger/notice.rb', line 115

def params
  @params
end

#request_idObject

The ID of the request which caused this notice.



148
149
150
# File 'lib/honeybadger/notice.rb', line 148

def request_id
  @request_id
end

#sessionObject

A hash of session data from the request.



127
128
129
# File 'lib/honeybadger/notice.rb', line 127

def session
  @session
end

#sourceObject (readonly)

Deprecated: Excerpt from source file.



139
140
141
# File 'lib/honeybadger/notice.rb', line 139

def source
  @source
end

#tagsObject

Tags which will be applied to error.



97
98
99
# File 'lib/honeybadger/notice.rb', line 97

def tags
  @tags
end

#urlObject

The URL at which the error occurred (if any).



130
131
132
# File 'lib/honeybadger/notice.rb', line 130

def url
  @url
end

Instance Method Details

#as_json(*args) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Template used to create JSON payload.

Returns:

  • (Hash)

    JSON representation of notice.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/honeybadger/notice.rb', line 240

def as_json(*args)
  request = construct_request_hash
  request[:context] = s(context)
  request[:local_variables] = local_variables if local_variables

  {
    api_key: s(api_key),
    notifier: NOTIFIER,
    breadcrumbs: sanitized_breadcrumbs,
    error: {
      token: id,
      class: s(error_class),
      message: s(error_message),
      backtrace: s(parsed_backtrace),
      fingerprint: fingerprint_hash,
      tags: s(tags),
      causes: s(prepare_causes(causes))
    },
    details: s(details),
    request: request,
    server: {
      project_root: s(config[:root]),
      revision: s(config[:revision]),
      environment_name: s(config[:env]),
      hostname: s(config[:hostname]),
      stats: stats,
      time: now,
      pid: pid
    },
    correlation_context: {
      request_id: s(request_id)
    }
  }
end

#halt!Object

Halts the notice and the before_notify callback chain.

Returns nothing.



291
292
293
# File 'lib/honeybadger/notice.rb', line 291

def halt!
  @halted ||= true
end

#halted?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if this notice will be discarded.

Returns:

  • (Boolean)


297
298
299
# File 'lib/honeybadger/notice.rb', line 297

def halted?
  !!@halted
end

#ignore?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if this notice should be ignored.

Returns:

  • (Boolean)


284
285
286
# File 'lib/honeybadger/notice.rb', line 284

def ignore?
  ignore_by_origin? || ignore_by_class? || ignore_by_callbacks?
end

#parsed_backtraceArray<{:number, :file, :method => String}>

The parsed exception backtrace. Lines in this backtrace that are from installed gems have the base path for gem installs replaced by “[GEM_ROOT]”, while those in the project have “[PROJECT_ROOT]”.

Returns:

  • (Array<{:number, :file, :method => String}>)


154
155
156
# File 'lib/honeybadger/notice.rb', line 154

def parsed_backtrace
  @parsed_backtrace ||= parse_backtrace(backtrace)
end

#to_json(*a) ⇒ Hash

Converts the notice to JSON.

Returns:

  • (Hash)

    The JSON representation of the notice.



278
279
280
# File 'lib/honeybadger/notice.rb', line 278

def to_json(*a)
  ::JSON.generate(as_json(*a))
end