Class: Rnotifier::ExceptionData

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

Constant Summary collapse

HEADER_REGX =
/^HTTP_/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, env, options = {}) ⇒ ExceptionData

Returns a new instance of ExceptionData.



5
6
7
8
9
10
11
12
13
# File 'lib/rnotifier/exception_data.rb', line 5

def initialize(exception, env, options = {})
  @exception = exception
  @options   = options  
  if options[:type] == :rack
    @request = Rack::Request.new(env)
  else
    @env = env
  end
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



3
4
5
# File 'lib/rnotifier/exception_data.rb', line 3

def env
  @env
end

#exceptionObject (readonly)

Returns the value of attribute exception.



3
4
5
# File 'lib/rnotifier/exception_data.rb', line 3

def exception
  @exception
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/rnotifier/exception_data.rb', line 3

def options
  @options
end

#requestObject (readonly)

Returns the value of attribute request.



3
4
5
# File 'lib/rnotifier/exception_data.rb', line 3

def request
  @request
end

Instance Method Details

#exception_dataObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/rnotifier/exception_data.rb', line 51

def exception_data
  e_data = {
    :class_name => exception.class.to_s,
    :message    => exception.message,
    :backtrace  => exception.backtrace,
    :fingerprint => (self.fingerprint rescue nil)
  }
  e_data[:code] = ExceptionCode.get(exception) if Config.capture_code
  e_data
end

#filtered_paramsObject



70
71
72
73
74
75
76
# File 'lib/rnotifier/exception_data.rb', line 70

def filtered_params
  if rp = request.env['action_dispatch.parameter_filter'] 
    ParameterFilter.filter(request.env['action_dispatch.request.parameters'] || request.params, rp)
  else 
    ParameterFilter.default_filter(request.params)
  end
end

#fingerprintObject



62
63
64
65
66
67
68
# File 'lib/rnotifier/exception_data.rb', line 62

def fingerprint
  #data[:fingerprint] = Digest::MD5.hexdigest("#{exception.message.gsub(/#<\w*:\w*>/, '')}#{data[:fingerprint]}")

  if exception.backtrace && !exception.backtrace.empty?
    Digest::MD5.hexdigest(exception.backtrace.join)
  end
end

#headersObject



80
81
82
83
84
85
86
87
# File 'lib/rnotifier/exception_data.rb', line 80

def headers
  headers = {}
  request.env.each do |k, v| 
    headers[k.sub(HEADER_REGX, '').downcase] = v if k =~ HEADER_REGX 
  end
  headers['cookie'] = headers['cookie'].sub(/_session=\S+/, '_session=[FILTERED]') if headers['cookie']
  headers
end

#is_bot?(agent) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
# File 'lib/rnotifier/exception_data.rb', line 89

def is_bot?(agent)
  return false if agent.nil? || Config.ignore_bots.nil? || Config.ignore_bots.empty? 
  
  Config.ignore_bots.each { |bot| return true if (agent =~ Regexp.new(bot)) }
  false
end

#notifyObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rnotifier/exception_data.rb', line 15

def notify
  return false unless Config.valid?
  return false if Config.ignore_exceptions && Config.ignore_exceptions.include?(exception.class.to_s)
  return false if @options[:type] == :rack && is_bot?(@request.user_agent)

  begin
    data = options[:type] == :rack ? self.rack_exception_data : {:extra => self.env }
    data[:app_env] = Rnotifier::Config.app_env
    data[:occurred_at] = Time.now.to_i
    data[:exception] = self.exception_data
    data[:context_data] = Thread.current[:rnotifier_context] if Thread.current[:rnotifier_context]
    data[:data_from] = options[:type]
    data[:rnotifier_client] = Config::CLIENT 

    return Notifier.send(data, Config.exception_path)
  rescue Exception => e
    Rlogger.error("[NOTIFY] #{e.message}")
    Rlogger.error("[NOTIFY] #{e.backtrace}")
    false
  end
end

#rack_exception_dataObject



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

def rack_exception_data
  data = {}
  data[:request] = {
    :url          => request.url,
    :referer_url  => request.referer,
    :ip           => request.ip,
    :http_method  => "#{request.request_method}#{' # XHR' if request.xhr?}",
    :params       => filtered_params,
    :headers      => self.headers,
    :session      => request.session
  }
  data
end