Class: Exceptional::ExceptionData

Inherits:
Object
  • Object
show all
Defined in:
lib/exceptional/exception_data.rb

Direct Known Subclasses

ControllerExceptionData, RackExceptionData

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, name = nil) ⇒ ExceptionData

Returns a new instance of ExceptionData.



5
6
7
8
# File 'lib/exceptional/exception_data.rb', line 5

def initialize(exception, name=nil)
  @exception = exception
  @name = name
end

Class Method Details

.sanitize_hash(hash) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/exceptional/exception_data.rb', line 56

def self.sanitize_hash(hash)
  case hash
    when Hash
      hash.inject({}) do |result, (key, value)|
        result.update(key => sanitize_hash(value))
      end
    when Fixnum, Array, String, Bignum
      hash
    else
      hash.to_s
  end
rescue
  {}
end

Instance Method Details

#context_stuffObject



29
30
31
32
# File 'lib/exceptional/exception_data.rb', line 29

def context_stuff
  context = Thread.current[:exceptional_context]
  (context.nil? || context.empty?) ? {} : {'context' => context}
end

#extra_stuffObject



25
26
27
# File 'lib/exceptional/exception_data.rb', line 25

def extra_stuff
  { 'rescue_block' => { 'name' => @name} }
end

#frameworkObject



47
48
49
# File 'lib/exceptional/exception_data.rb', line 47

def framework
  nil
end

#to_hashObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/exceptional/exception_data.rb', line 10

def to_hash
  hash = ::Exceptional::ApplicationEnvironment.to_hash(framework)
  hash.merge!({
    'exception' => {
      'exception_class' => @exception.class.to_s,
      'message' => @exception.message,
      'backtrace' => @exception.backtrace,
      'occurred_at' => Time.now.strftime("%Y%m%d %H:%M:%S %Z")
    }
  })
  hash.merge!(extra_stuff)
  hash.merge!(context_stuff)
  self.class.sanitize_hash(hash)
end

#to_jsonObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/exceptional/exception_data.rb', line 34

def to_json
  begin
    to_hash.to_json
  rescue NoMethodError
    begin
      require 'json'
      return to_hash.to_json
    rescue StandardError
      raise StandardError.new("You need a json gem/library installed to send errors to Exceptional (Object.to_json not defined). \nInstall json_pure, yajl-ruby, json-jruby, or the c-based json gem")
    end
  end
end

#uniqueness_hashObject



51
52
53
54
# File 'lib/exceptional/exception_data.rb', line 51

def uniqueness_hash
  return nil if (@exception.backtrace.nil? || @exception.backtrace.empty?)
  Digest::MD5.hexdigest(@exception.backtrace.join)
end