Class: Exceptional::ExceptionData

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, name = nil) ⇒ ExceptionData

Returns a new instance of ExceptionData.



7
8
9
10
# File 'lib/exceptional/exception_data.rb', line 7

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

Class Method Details

.sanitize_hash(hash) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/exceptional/exception_data.rb', line 60

def self.sanitize_hash(hash)
        
  case hash
    when Hash
      hash.inject({}) do |result, (key, value)|            
        result.update(key => sanitize_hash(value))
      end
    when Array
      hash.collect{|value| sanitize_hash(value)}
    when Fixnum, String, Bignum
      hash
    else
      hash.to_s
  end
rescue Exception => e
  Exceptional.logger.error(hash)
  Exceptional.logger.error(e.message)
  Exceptional.logger.error(e.backtrace)      
  {}
end

.sanitize_session(request) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/exceptional/exception_data.rb', line 93

def self.sanitize_session(request)
  session_hash = {'session_id' => "", 'data' => {}}

  if request.respond_to?(:session)      
    session = request.session
    session_hash['session_id'] = request.session_options ? request.session_options[:id] : nil
    session_hash['session_id'] ||= session.respond_to?(:session_id) ? session.session_id : session.instance_variable_get("@session_id")
    session_hash['data'] = session.respond_to?(:to_hash) ? session.to_hash : session.instance_variable_get("@data") || {}
    session_hash['session_id'] ||= session_hash['data'][:session_id]
    session_hash['data'].delete(:session_id)
  end

  self.sanitize_hash(session_hash)
end

Instance Method Details

#context_stuffObject



31
32
33
34
# File 'lib/exceptional/exception_data.rb', line 31

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

#extra_stuffObject



27
28
29
# File 'lib/exceptional/exception_data.rb', line 27

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

#extract_http_headers(env) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/exceptional/exception_data.rb', line 81

def extract_http_headers(env)
  headers = {}
  env.select{|k, v| k =~ /^HTTP_/}.each do |name, value|
    proper_name = name.sub(/^HTTP_/, '').split('_').map{|upper_case| upper_case.capitalize}.join('-')
    headers[proper_name] = value
  end
  unless headers['Cookie'].nil?
    headers['Cookie'] = headers['Cookie'].sub(/_session=\S+/, '_session=[FILTERED]')
  end
  headers
end

#frameworkObject



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

def framework
  nil
end

#to_hashObject



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

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.utc.iso8601
    }
  })
  hash.merge!(extra_stuff)
  hash.merge!(context_stuff)
  self.class.sanitize_hash(hash)
end

#to_jsonObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/exceptional/exception_data.rb', line 36

def to_json
  begin
    to_hash.to_json
  rescue NoMethodError
    begin
      require 'json'
      return to_hash.to_json
    rescue StandardError => e                   
      Exceptional.logger.error(e.message)
      Exceptional.logger.error(e.backtrace)                    
      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



55
56
57
58
# File 'lib/exceptional/exception_data.rb', line 55

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