Skip to content

Commit

Permalink
pass exception object to error block, fixes #323
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed Jul 24, 2011
1 parent 9d29e6f commit 5b9d3da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -700,12 +700,12 @@ def route_eval
# 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)|
Expand Down Expand Up @@ -812,21 +812,22 @@ def handle_exception!(boom)
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)
Expand Down
9 changes: 9 additions & 0 deletions test/mapped_error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ def test_default
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
Expand Down

0 comments on commit 5b9d3da

Please sign in to comment.