2009-05-03 00:02:22 -04:00
|
|
|
module ActionDispatch
|
|
|
|
class Rescue
|
2009-09-15 17:33:15 -04:00
|
|
|
def initialize(app, rescuers = {}, &block)
|
|
|
|
@app, @rescuers = app, {}
|
|
|
|
rescuers.each { |exception, rescuer| rescue_from(exception, rescuer) }
|
|
|
|
instance_eval(&block) if block_given?
|
2009-05-03 00:02:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
@app.call(env)
|
|
|
|
rescue Exception => exception
|
2009-09-15 17:33:15 -04:00
|
|
|
if rescuer = @rescuers[exception.class.name]
|
|
|
|
env['action_dispatch.rescue.exception'] = exception
|
|
|
|
rescuer.call(env)
|
|
|
|
else
|
|
|
|
raise exception
|
|
|
|
end
|
2009-05-03 00:02:22 -04:00
|
|
|
end
|
2009-09-15 17:33:15 -04:00
|
|
|
|
|
|
|
protected
|
|
|
|
def rescue_from(exception, rescuer)
|
|
|
|
exception = exception.class.name if exception.is_a?(Exception)
|
|
|
|
@rescuers[exception.to_s] = rescuer
|
|
|
|
end
|
2009-05-03 00:02:22 -04:00
|
|
|
end
|
|
|
|
end
|