Class: Failbot::Rescuer

Inherits:
Object
  • Object
show all
Defined in:
lib/failbot/middleware.rb

Overview

Rack middleware that rescues exceptions raised from the downstream app and reports to failbot. The exception is reraised after being sent to haystack so upstream middleware can still display an error page or whathaveyou.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, other) ⇒ Rescuer

Returns a new instance of Rescuer.



6
7
8
9
# File 'lib/failbot/middleware.rb', line 6

def initialize(app, other)
  @app   = app
  @other = other
end

Class Method Details

.context(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/failbot/middleware.rb', line 25

def self.context(env)
  request = Rack::Request.new(env.dup)
  params = {}
  begin
    params = request.params
  rescue
    env["rack.input"].rewind
  end

  {
    :method       => request.request_method,
    :user_agent   => env['HTTP_USER_AGENT'],
    :params       => filtered_parameters(env, params),
    :session      => filtered_parameters(env, (request.session.to_hash rescue {})),
    :referrer     => request.referrer,
    :remote_ip    => request.ip,
    :url          => request.url
  }
end

.filter_parameters(filter_words, unfiltered_parameters) ⇒ Object

Borrowed from Rails.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/failbot/middleware.rb', line 58

def self.filter_parameters(filter_words, unfiltered_parameters)
  parameter_filter = Regexp.new(filter_words.collect{ |s| s.to_s }.join('|'), true) if filter_words.length > 0

  filtered_parameters = {}

  unfiltered_parameters.each do |key, value|
    if key =~ parameter_filter
      filtered_parameters[key] = '[FILTERED]'
    elsif value.is_a?(Hash)
      filtered_parameters[key] = filter_parameters(filter_words, value)
    elsif value.is_a?(Array)
      filtered_parameters[key] = value.collect do |item|
        case item
        when Hash, Array
          filter_parameters(filter_words, item)
        else
          item
        end
      end
    elsif block_given?
      key = key.dup
      value = value.dup if value.duplicable?
      yield key, value
      filtered_parameters[key] = value
    else
      filtered_parameters[key] = value
    end
  end

  filtered_parameters
end

.filtered_parameters(env, params) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/failbot/middleware.rb', line 49

def self.filtered_parameters(env, params)
  if env['filter_words']
    filter_parameters(env['filter_words'], params)
  else
    params
  end
end

.report(exception, env, other = {}) ⇒ Object



45
46
47
# File 'lib/failbot/middleware.rb', line 45

def self.report(exception, env, other = {})
  Failbot.report(exception, other.merge(context(env)))
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  Failbot.reset!
  Failbot.push @other.merge(self.class.context(env)) do
    begin
      start = Time.now
      @app.call(env)
    rescue Object => boom
      elapsed = Time.now - start
      Failbot.report(boom, {:time => elapsed.to_s})
      raise
    end
  end
end