Class: ErrorStalker::ExceptionReport

Inherits:
Object
  • Object
show all
Defined in:
lib/error_stalker/exception_report.rb

Overview

An ExceptionReport contains all of the information we have on an exception, which can then be transformed into whatever format is needed for further investigation. Some data stores may override this class, but they should be able to be treated as instances of this class regardless.

Constant Summary collapse

STACK_DIGEST_LENGTH =

The number of characters in this exception’s stacktrace that should be used to uniquify this exception. Exceptions raised from the same place should have the same stacktrace, up to STACK_DIGEST_LENGTH characters.

4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ ExceptionReport

Build a new ExceptionReport. params[:application] is a string identifying the application or component the exception was sent from, params[:exception] is the exception object you want to report (or a string error message), and params[:data] is any extra arbitrary data you want to log along with this report.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/error_stalker/exception_report.rb', line 39

def initialize(params = {})
  params = symbolize_keys(params)
  @id = params[:id]
  @application = params[:application]
  @machine = params[:machine] || machine_name
  @timestamp = params[:timestamp] || Time.now
  @type = params[:type] || params[:exception].class.name

  if params[:exception].is_a?(Exception)
    @exception = params[:exception].to_s
  else
    @exception = params[:exception]
  end

  @data = params[:data]

  if params[:backtrace]
    @backtrace = params[:backtrace]
  else
    @backtrace = params[:exception].backtrace if params[:exception].is_a?(Exception)
  end

  @digest = params[:digest] if params[:digest]
end

Instance Attribute Details

#applicationObject (readonly)

The name of the application that caused this exception.



12
13
14
# File 'lib/error_stalker/exception_report.rb', line 12

def application
  @application
end

#backtraceObject (readonly)

The backtrace corresponding to this exception. Should be an array of strings, each referring to a single stack frame.



28
29
30
# File 'lib/error_stalker/exception_report.rb', line 28

def backtrace
  @backtrace
end

#exceptionObject (readonly)

The exception object (or string message) this report represents



24
25
26
# File 'lib/error_stalker/exception_report.rb', line 24

def exception
  @exception
end

#idObject

A unique identifier for this exception



31
32
33
# File 'lib/error_stalker/exception_report.rb', line 31

def id
  @id
end

#machineObject (readonly)

The name of the machine that raised this exception.



15
16
17
# File 'lib/error_stalker/exception_report.rb', line 15

def machine
  @machine
end

#timestampObject (readonly)

The time that this exception occurred



18
19
20
# File 'lib/error_stalker/exception_report.rb', line 18

def timestamp
  @timestamp
end

#typeObject (readonly)

The class name of exception



21
22
23
# File 'lib/error_stalker/exception_report.rb', line 21

def type
  @type
end

Instance Method Details

#dataObject

A hash of extra data logged along with this exception.



79
80
81
# File 'lib/error_stalker/exception_report.rb', line 79

def data
  @data_with_fixed_encoding ||= JSON.parse(raw_data.to_json.encode('utf-8', 'ascii-8bit', :invalid => :replace, :undef => :replace))
end

#digestObject

Generate a ‘mostly-unique’ hash code for this exception, that should be the same for similar exceptions and different for different exceptions. This is used to group similar exceptions together.



74
75
76
# File 'lib/error_stalker/exception_report.rb', line 74

def digest
  @digest ||= Digest::MD5.hexdigest((backtrace ? backtrace.to_s[0,STACK_DIGEST_LENGTH] : exception.to_s) + type.to_s)
end

#raw_dataObject

The extra data associated with this object, without fixing any broken encodings.



85
86
87
# File 'lib/error_stalker/exception_report.rb', line 85

def raw_data
  @data
end

#to_jsonObject

Serialize this object to json, so we can send it over the wire.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/error_stalker/exception_report.rb', line 90

def to_json
  {
    :application => application,
    :machine => machine,
    :timestamp => timestamp,
    :type => type,
    :exception => exception,
    :data => raw_data,
    :backtrace => backtrace
  }.to_json
end