Class: Rack::RouteExceptions

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/contrib/route_exceptions.rb

Constant Summary collapse

ROUTES =
[
  [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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RouteExceptions

Returns a new instance of RouteExceptions.



11
12
13
# File 'lib/rack/contrib/route_exceptions.rb', line 11

def initialize(app)
  @app = app
end

Class Method Details

.route(exception, to) ⇒ Object



43
44
45
46
# File 'lib/rack/contrib/route_exceptions.rb', line 43

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

Instance Method Details

#call(env, try_again = true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/contrib/route_exceptions.rb', line 15

def call(env, try_again = true)
  status, header, body = response = @app.call(env)

  response
rescue Exception => exception
  raise(exception) unless try_again

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

  raise(exception)
end

#route(to, env, response, exception) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rack/contrib/route_exceptions.rb', line 30

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)

  env['PATH_INFO'] = to

  call(env, try_again = false)
end