Class: Honeybadger::Notice

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

Constant Summary

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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



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

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)

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

  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)

Public: The API key used to deliver this notice.



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

def api_key
  @api_key
end

#backtraceObject (readonly)

Public: The backtrace from the given exception or hash.



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

def backtrace
  @backtrace
end

#error_classObject (readonly)

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



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

def error_class
  @error_class
end

#error_messageObject (readonly)

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



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

def error_message
  @error_message
end

#exceptionObject (readonly)

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



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

def exception
  @exception
end

#fingerprintObject (readonly)

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



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

def fingerprint
  @fingerprint
end

#idObject (readonly)

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



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

def id
  @id
end

#local_variablesObject (readonly)

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



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

def local_variables
  @local_variables
end

#sourceObject (readonly)

Deprecated: Excerpt from source file.



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

def source
  @source
end

#tagsObject (readonly)

Public: Tags which will be applied to error.



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

def tags
  @tags
end

Class Method Details

.sessionObject



495
496
497
# File 'lib/honeybadger/notice.rb', line 495

def @request.session
  @table[:session]
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



214
215
216
217
218
219
220
221
# File 'lib/honeybadger/notice.rb', line 214

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

#actionObject

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



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

def action; @request[:action]; end

#as_json(*args) ⇒ Object

Internal: Template used to create JSON payload

Returns Hash JSON representation of notice



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

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

Public: CGI variables such as HTTP_METHOD.



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

def cgi_data; @request[:cgi_data]; end

#componentObject Also known as: controller

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



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

def component; @request[:component]; end

#ignore?Boolean

Internal: Determines if this notice should be ignored



224
225
226
# File 'lib/honeybadger/notice.rb', line 224

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

#paramsObject Also known as: parameters

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



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

def params; @request[:params]; end

#sessionObject



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

def session; @request[:session]; end

#to_json(*a) ⇒ Object

Public: Creates JSON

Returns valid JSON representation of Notice



201
202
203
# File 'lib/honeybadger/notice.rb', line 201

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

#urlObject

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



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

def url; @request[:url]; end