Permalink
Browse files

A little bit of refactoring for RouteExceptions

  • Loading branch information...
1 parent 5cfaaf8 commit 3b0bb91f4246735393cb332a2c590b3fc6ae3442 @manveru manveru committed May 1, 2009
Showing with 20 additions and 19 deletions.
  1. +20 −19 lib/rack/contrib/route_exceptions.rb
@@ -4,45 +4,46 @@ class RouteExceptions
[Exception, '/error/internal']
]
- ROUTE_EXCEPTIONS_PATH_INFO = 'rack.route_exceptions.path_info'.freeze
- ROUTE_EXCEPTIONS_EXCEPTION = 'rack.route_exceptions.exception'.freeze
- ROUTE_EXCEPTIONS_RESPONSE = 'rack.route_exceptions.response'.freeze
+ 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)
- status, header, body = response = @app.call(env)
-
- response
+ 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, response, exception)
+ return route(to, env, returned, exception)
end
raise(exception)
end
- def route(to, env, response, exception)
- hash = {
- ROUTE_EXCEPTIONS_PATH_INFO => env['PATH_INFO'],
- ROUTE_EXCEPTIONS_EXCEPTION => exception,
- ROUTE_EXCEPTIONS_RESPONSE => response
- }
- env.merge!(hash)
+ 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
-
- def self.route(exception, to)
- ROUTES.delete_if{|k,v| k == exception }
- ROUTES << [exception, to]
- end
end
end

0 comments on commit 3b0bb91

Please sign in to comment.