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.

','.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.

/[^\w]/.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

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


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/honeybadger/notice.rb', line 171

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|
    "#{exception.class.name}: #{exception.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.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.


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

def action
  @action
end

#api_keyObject

The API key used to deliver this notice.


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

def api_key
  @api_key
end

#backtraceObject

The backtrace from the given exception or hash.


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

def backtrace
  @backtrace
end

Returns The collection of captured breadcrumbs

Returns:


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

def breadcrumbs
  @breadcrumbs
end

#causeObject

The exception cause if available.


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

def cause
  @cause
end

#causesCause (readonly)

Returns A list of exception causes (see Cause)

Returns:

  • (Cause)

    A list of exception causes (see Cause)


84
85
86
# File 'lib/honeybadger/notice.rb', line 84

def causes
  @causes
end

#cgi_dataObject

CGI variables such as HTTP_METHOD.


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

def cgi_data
  @cgi_data
end

#componentObject Also known as: controller

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


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

def component
  @component
end

#contextObject

The context Hash.


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

def context
  @context
end

#error_classObject

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


99
100
101
# File 'lib/honeybadger/notice.rb', line 99

def error_class
  @error_class
end

#error_messageObject

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


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

def error_message
  @error_message
end

#exceptionObject (readonly)

The exception that caused this notice, if any.


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

def exception
  @exception
end

#fingerprintObject

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


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

def fingerprint
  @fingerprint
end

#idObject (readonly)

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


71
72
73
# File 'lib/honeybadger/notice.rb', line 71

def id
  @id
end

#local_variablesObject

Local variables are extracted from first frame of backtrace.


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

def local_variables
  @local_variables
end

#paramsObject Also known as: parameters

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


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

def params
  @params
end

#sessionObject

A hash of session data from the request.


122
123
124
# File 'lib/honeybadger/notice.rb', line 122

def session
  @session
end

#sourceObject (readonly)

Deprecated: Excerpt from source file.


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

def source
  @source
end

#tagsObject

Tags which will be applied to error.


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

def tags
  @tags
end

#urlObject

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


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

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.


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/honeybadger/notice.rb', line 216

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(parse_backtrace(backtrace)),
      fingerprint: fingerprint_hash,
      tags: s(tags),
      causes: s(prepare_causes(causes))
    },
    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
    }
  }
end

#halt!Object

Halts the notice and the before_notify callback chain.

Returns nothing.


263
264
265
# File 'lib/honeybadger/notice.rb', line 263

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)

269
270
271
# File 'lib/honeybadger/notice.rb', line 269

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)

256
257
258
# File 'lib/honeybadger/notice.rb', line 256

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

#to_json(*a) ⇒ Hash

Converts the notice to JSON.

Returns:

  • (Hash)

    The JSON representation of the notice.


250
251
252
# File 'lib/honeybadger/notice.rb', line 250

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