Class: Honeybadger::Notice

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

Constant Summary collapse

TAG_SEPERATOR =

Internal: The String character used to split tag strings.

','.freeze
TAG_SANITIZER =

Internal: The Regexp used to strip invalid characters from individual tags.

/[^\w]/.freeze
PROJECT_ROOT_CACHE =

Internal: Cache project path substitutions for backtrace lines.

{}
GEM_ROOT_CACHE =

Internal: Cache gem path substitutions for backtrace lines.

{}
BACKTRACE_FILTERS =

Internal: 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

Constructor Details

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

Returns a new instance of Notice.


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

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)
  Util::RequestPayload::KEYS.each do |key|
    self.send(:"#{key}=", request[key])
  end

  self.params ||= {}
  self.session ||= {}
  self.cgi_data ||= {}

  @context = construct_context_hash(opts)

  @causes = unwrap_causes(@exception)

  @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]

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

Instance Attribute Details

#actionObject

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


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

def action
  @action
end

#api_keyObject

Public: The API key used to deliver this notice.


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

def api_key
  @api_key
end

#backtraceObject

Public: The backtrace from the given exception or hash.


49
50
51
# File 'lib/honeybadger/notice.rb', line 49

def backtrace
  @backtrace
end

#cgi_dataObject

Public: CGI variables such as HTTP_METHOD.


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

def cgi_data
  @cgi_data
end

#componentObject Also known as: controller

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


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

def component
  @component
end

#error_classObject

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


58
59
60
# File 'lib/honeybadger/notice.rb', line 58

def error_class
  @error_class
end

#error_messageObject

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


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

def error_message
  @error_message
end

#exceptionObject (readonly)

Public: The exception that caused this notice, if any.


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

def exception
  @exception
end

#fingerprintObject

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


52
53
54
# File 'lib/honeybadger/notice.rb', line 52

def fingerprint
  @fingerprint
end

#idObject (readonly)

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


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

def id
  @id
end

#local_variablesObject (readonly)

Public: Local variables are extracted from first frame of backtrace.


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

def local_variables
  @local_variables
end

#paramsObject Also known as: parameters

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


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

def params
  @params
end

#sessionObject

Public: A hash of session data from the request.


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

def session
  @session
end

#sourceObject (readonly)

Deprecated: Excerpt from source file.


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

def source
  @source
end

#tagsObject

Public: Tags which will be applied to error.


55
56
57
# File 'lib/honeybadger/notice.rb', line 55

def tags
  @tags
end

#urlObject

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


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

def url
  @url
end

Instance Method Details

#[](method) ⇒ Object

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

method - The given key for an attribute.

Examples

notice[:error_message]

Returns the attribute value, or self if given `:request`.


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

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

#as_json(*args) ⇒ Object

Internal: Template used to create JSON payload.

Returns Hash JSON representation of notice.


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
211
212
213
214
215
# File 'lib/honeybadger/notice.rb', line 175

def as_json(*args)
  request = {
    url: url,
    component: component,
    action: action,
    params: params,
    session: session,
    cgi_data: cgi_data
  }
  request.delete_if {|k,v| config.excluded_request_keys.include?(k) }

  request[:sanitizer] = request_sanitizer
  request = Util::RequestPayload.build(request)

  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

#ignore?Boolean

Internal: Determines if this notice should be ignored.

Returns:

  • (Boolean)

243
244
245
# File 'lib/honeybadger/notice.rb', line 243

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

#to_json(*a) ⇒ Object

Public: Creates JSON.

Returns valid JSON representation of Notice.


220
221
222
# File 'lib/honeybadger/notice.rb', line 220

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