Class: Honeybadger::Notice

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

Constant Summary

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

Class Method 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



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
# File 'lib/honeybadger/notice.rb', line 145

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

  @opts = opts
  @config = config

  @rack_env = opts.fetch(:rack_env, nil)

  @request_sanitizer = Util::Sanitizer.new(filters: params_filters)

  @exception = unwrap_exception(opts[:exception])
  @error_class = exception_attribute(:error_class, 'Notice') {|exception| exception.class.name }
  @error_message = exception_attribute(:error_message, 'No message provided') do |exception|
    "#{exception.class.name}: #{exception.message}"
  end
  @backtrace = parse_backtrace(exception_attribute(:backtrace, caller))

  @request = construct_request_hash(config, opts)

  @context = construct_context_hash(opts, exception)

  @cause = opts[:cause] || exception_cause(@exception) || $!
  @causes = unwrap_causes(@cause)

  @tags = construct_tags(opts[:tags])
  @tags = construct_tags(context[:tags]) | @tags if context

  @stats = Util::Stats.all

  @local_variables = local_variables_from_exception(exception, config)

  @api_key = opts[:api_key] || config[:api_key]

  monkey_patch_action_dispatch_test_process!

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

Instance Attribute Details

#api_keyObject (readonly)

The API key used to deliver this notice.



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

def api_key
  @api_key
end

#backtraceObject (readonly)

The backtrace from the given exception or hash.



69
70
71
# File 'lib/honeybadger/notice.rb', line 69

def backtrace
  @backtrace
end

#causeObject (readonly)

The exception cause if available.



66
67
68
# File 'lib/honeybadger/notice.rb', line 66

def cause
  @cause
end

#error_classObject (readonly)

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



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

def error_class
  @error_class
end

#error_messageObject (readonly)

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



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

def error_message
  @error_message
end

#exceptionObject (readonly)

The exception that caused this notice, if any.



63
64
65
# File 'lib/honeybadger/notice.rb', line 63

def exception
  @exception
end

#fingerprintObject (readonly)

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



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

def fingerprint
  @fingerprint
end

#idObject (readonly)

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



60
61
62
# File 'lib/honeybadger/notice.rb', line 60

def id
  @id
end

#local_variablesObject (readonly)

Local variables are extracted from first frame of backtrace.



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

def local_variables
  @local_variables
end

#sourceObject (readonly)

Deprecated: Excerpt from source file.



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

def source
  @source
end

#tagsObject (readonly)

Tags which will be applied to error.



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

def tags
  @tags
end

Class Method Details

.sessionObject



531
532
533
# File 'lib/honeybadger/notice.rb', line 531

def @request.session
  @table[:session]
end

Instance Method Details

#[](method) ⇒ Object

Allows properties to be accessed using a hash-like syntax.

Examples:

notice[:error_message]

Parameters:

  • method (Symbol)

    The given key for an attribute.

Returns:

  • (Object)

    The attribute value.



234
235
236
237
238
239
240
241
# File 'lib/honeybadger/notice.rb', line 234

def [](method)
  case method
  when :request
    self
  else
    send(method)
  end
end

#actionObject

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



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

def action; @request[:action]; end

#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.



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
# File 'lib/honeybadger/notice.rb', line 190

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

  {
    api_key: s(api_key),
    notifier: NOTIFIER,
    error: {
      token: id,
      class: s(error_class),
      message: s(error_message),
      backtrace: s(backtrace.to_a),
      fingerprint: s(fingerprint),
      tags: s(tags),
      causes: s(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

#cgi_dataObject

CGI variables such as HTTP_METHOD.



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

def cgi_data; @request[:cgi_data]; end

#componentObject Also known as: controller

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



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

def component; @request[:component]; 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)


245
246
247
# File 'lib/honeybadger/notice.rb', line 245

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

#paramsObject Also known as: parameters

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



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

def params; @request[:params]; end

#sessionObject



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

def session; @request[:session]; end

#to_json(*a) ⇒ Hash

Converts the notice to JSON.

Returns:

  • (Hash)

    The JSON representation of the notice.



222
223
224
# File 'lib/honeybadger/notice.rb', line 222

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

#urlObject

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



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

def url; @request[:url]; end