Sha256: be0bfbb18ed2ef7644869e792e7c3c0f3ca8fcf223a40cfc8a024a77b0c105cf

Contents?: true

Size: 1.03 KB

Versions: 34

Compression:

Stored size: 1.03 KB

Contents

module Rack
  class RouteExceptions
    ROUTES = [
      [Exception, '/error/internal']
    ]

    PATH_INFO = 'rack.route_exceptions.path_info'.freeze
    EXCEPTION = 'rack.route_exceptions.exception'.freeze
    RETURNED  = 'rack.route_exceptions.returned'.freeze

    class << self
      def route(exception, to)
        ROUTES.delete_if{|k,v| k == exception }
        ROUTES << [exception, to]
      end

      alias []= route
    end

    def initialize(app)
      @app = app
    end

    def call(env, try_again = true)
      returned = @app.call(env)
    rescue Exception => exception
      raise(exception) unless try_again

      ROUTES.each do |klass, to|
        next unless klass === exception
        return route(to, env, returned, exception)
      end

      raise(exception)
    end

    def route(to, env, returned, exception)
      env.merge!(
        PATH_INFO => env['PATH_INFO'],
        EXCEPTION => exception,
        RETURNED => returned
      )

      env['PATH_INFO'] = to

      call(env, try_again = false)
    end
  end
end

Version data entries

34 entries across 34 versions & 8 rubygems

Version Path
rack-contrib-2.2.0 lib/rack/contrib/route_exceptions.rb
rack-contrib-2.1.0 lib/rack/contrib/route_exceptions.rb
rack-contrib-2.0.1 lib/rack/contrib/route_exceptions.rb
rack-contrib-2.0.0 lib/rack/contrib/route_exceptions.rb
rack-contrib-1.8.0 lib/rack/contrib/route_exceptions.rb
rack-contrib-1.7.0 lib/rack/contrib/route_exceptions.rb
rack-contrib-1.6.0 lib/rack/contrib/route_exceptions.rb
rack-contrib-1.5.0 lib/rack/contrib/route_exceptions.rb
rack-contrib-1.4.0 lib/rack/contrib/route_exceptions.rb
rack-contrib-1.3.0 lib/rack/contrib/route_exceptions.rb
rack-contrib-1.2.0.39.g17d21b4 lib/rack/contrib/route_exceptions.rb
rack-contrib-1.2.0 lib/rack/contrib/route_exceptions.rb
cavalle-rack-contrib-1.0.0 lib/rack/contrib/route_exceptions.rb
rack-rack-contrib-0.9.2 lib/rack/contrib/route_exceptions.rb
tricycle-rack-contrib-0.9.3 lib/rack/contrib/route_exceptions.rb
rack-contrib-1.1.0 lib/rack/contrib/route_exceptions.rb
corntrace-rack-contrib-1.0.2.3 lib/rack/contrib/route_exceptions.rb
corntrace-rack-contrib-1.0.2.2 lib/rack/contrib/route_exceptions.rb
corntrace-rack-contrib-1.0.2.1 lib/rack/contrib/route_exceptions.rb
corntrace-rack-contrib-1.0.2 lib/rack/contrib/route_exceptions.rb