Sha256: faca4af6a19e8aeebb0b293b647f250ecff030df8de2fc116f0950dcba762fd1

Contents?: true

Size: 1.8 KB

Versions: 10

Compression:

Stored size: 1.8 KB

Contents

module ExceptionNotification
  class Rack
    class CascadePassException < Exception; end

    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

      @ignore_cascade_pass = options.delete(:ignore_cascade_pass) { true }

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

    def call(env)
      _, headers, _ = response = @app.call(env)

      if !@ignore_cascade_pass && headers['X-Cascade'] == 'pass'
        msg = "This exception means that the preceding Rack middleware set the 'X-Cascade' header to 'pass' -- in " <<
          "Rails, this often means that the route was not found (404 error)."
        raise CascadePassException, msg
      end

      response
    rescue Exception => exception
      if ExceptionNotifier.notify_exception(exception, :env => env)
        env['exception_notifier.delivered'] = true
      end

      raise exception unless exception.is_a?(CascadePassException)
      response
    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

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
exception_notification-4.2.0 lib/exception_notification/rack.rb
exception_notification-4.2.0.rc1 lib/exception_notification/rack.rb
exception_notification_more_info-1.0.1 lib/exception_notification/rack.rb
exception_notification_more_info-1.0.0 lib/exception_notification/rack.rb
exception_notification-4.1.4 lib/exception_notification/rack.rb
exception_notification-4.1.3 lib/exception_notification/rack.rb
exception_notification-4.1.2 lib/exception_notification/rack.rb
exception_notification-4.1.1 lib/exception_notification/rack.rb
exception_notification-4.1.0 lib/exception_notification/rack.rb
exception_notification-4.1.0.rc2 lib/exception_notification/rack.rb