Sha256: 3ca6fe563b98430fff0cd37b40250f04f39d82598e1a865851d181745cdfe316

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

module Rack
  class RouteExceptions
    ROUTES = []

    ROUTE_EXCEPTIONS_PATH_INFO = 'rack.route_exceptions.path_info'.freeze
    ROUTE_EXCEPTIONS_EXCEPTION = 'rack.route_exceptions.exception'.freeze
    ROUTE_EXCEPTIONS_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)
      hash = {
        ROUTE_EXCEPTIONS_PATH_INFO => env['PATH_INFO'],
        ROUTE_EXCEPTIONS_EXCEPTION => exception,
        ROUTE_EXCEPTIONS_RETURNED => returned
      }
      env.merge!(hash)

      env['PATH_INFO'] = to

      call(env, try_again = false)
    end
  end
end

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
Pistos-ramaze-2009.04.08 lib/vendor/route_exceptions.rb
manveru-ramaze-2009.04.01 lib/vendor/route_exceptions.rb
manveru-ramaze-2009.04.08 lib/vendor/route_exceptions.rb
manveru-ramaze-2009.04.18 lib/vendor/route_exceptions.rb
manveru-ramaze-2009.04.22 lib/vendor/route_exceptions.rb
manveru-ramaze-2009.04 lib/vendor/route_exceptions.rb
ramaze-2009.04 lib/vendor/route_exceptions.rb