Class: Rack::RouteExceptions

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

Constant Summary collapse

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 Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RouteExceptions

Returns a new instance of RouteExceptions.



20
21
22
# File 'lib/rack/contrib/route_exceptions.rb', line 20

def initialize(app)
  @app = app
end

Class Method Details

.[]=Object



17
18
19
20
# File 'lib/rack/contrib/route_exceptions.rb', line 17

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

.route(exception, to) ⇒ Object



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

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

Instance Method Details

#call(env, try_again = true) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rack/contrib/route_exceptions.rb', line 24

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

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



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rack/contrib/route_exceptions.rb', line 37

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