pass exception object to error block, fixes #323

This commit is contained in:
Konstantin Haase 2011-07-24 13:45:42 +02:00
parent 9d29e6ff60
commit 5b9d3dae7e
2 changed files with 16 additions and 6 deletions

View File

@ -700,12 +700,12 @@ module Sinatra
# Revert params afterwards.
#
# Returns pass block.
def process_route(pattern, keys, conditions, block = nil)
def process_route(pattern, keys, conditions, block = nil, values = [])
@original_params ||= @params
route = @request.path_info
route = '/' if route.empty? and not settings.empty_path_info?
if match = pattern.match(route)
values = match.captures.to_a.map { |v| force_encoding URI.decode(v) if v }
values += match.captures.to_a.map { |v| force_encoding URI.decode(v) if v }
params =
if keys.any?
keys.zip(values).inject({}) do |hash,(k,v)|
@ -812,21 +812,22 @@ module Sinatra
body '<h1>Not Found</h1>'
end
res = error_block!(boom.class) || error_block!(status)
res = error_block!(boom.class, boom) || error_block!(status, boom)
return res if res or not server_error?
raise boom if settings.raise_errors? or settings.show_exceptions?
error_block! Exception
error_block! Exception, boom
end
# Find an custom error block for the key(s) specified.
def error_block!(key)
def error_block!(key, *block_params)
base = settings
while base.respond_to?(:errors)
next base = base.superclass unless args = base.errors[key]
args += [block_params]
return process_route(*args)
end
return false unless key.respond_to? :superclass and key.superclass < Exception
error_block! key.superclass
error_block!(key.superclass, *block_params)
end
def dump_errors!(boom)

View File

@ -29,6 +29,15 @@ class MappedErrorTest < Test::Unit::TestCase
assert_equal 'Foo!', body
end
it 'passes the exception object to the error handler' do
mock_app do
set :raise_errors, false
error(FooError) { |e| assert_equal(FooError, e.class) }
get('/') { raise FooError }
end
get('/')
end
it 'uses the Exception handler if no matching handler found' do
mock_app {
set :raise_errors, false