File: rack.rb

package info (click to toggle)
ruby-exception-notification 4.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 808 kB
  • sloc: javascript: 7,680; ruby: 1,130; makefile: 2
file content (45 lines) | stat: -rw-r--r-- 1,299 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module ExceptionNotification
  class Rack
    def initialize(app, options = {})
      @app = app

      ExceptionNotifier.ignored_exceptions = options.delete(:ignore_exceptions) if options.key?(:ignore_exceptions)

      if options.key?(:ignore_if)
        rack_ignore = options.delete(:ignore_if)
        ExceptionNotifier.ignore_if do |exception, options|
          options.key?(:env) && rack_ignore.call(options[:env], exception)
        end
      end

      if options.key?(:ignore_crawlers)
        ignore_crawlers = options.delete(:ignore_crawlers)
        ExceptionNotifier.ignore_if do |exception, options|
          options.key?(:env) && from_crawler(options[:env], ignore_crawlers)
        end
      end

      options.each do |notifier_name, options|
        ExceptionNotifier.register_exception_notifier(notifier_name, options)
      end
    end

    def call(env)
      @app.call(env)
    rescue Exception => exception
      if ExceptionNotifier.notify_exception(exception, :env => env)
        env['exception_notifier.delivered'] = true
      end
      raise exception
    end

    private

    def from_crawler(env, ignored_crawlers)
      agent = env['HTTP_USER_AGENT']
      Array(ignored_crawlers).any? do |crawler|
        agent =~ Regexp.new(crawler)
      end
    end
  end
end