Permalink
Browse files

if an error handler is registered for some exception class, let it al…

…so handle subclasses
  • Loading branch information...
rkh committed Jun 4, 2011
1 parent 67ae9be commit 2e0030f115e983192b6ca7d3dc1f9ecb4867bf04
Showing with 32 additions and 1 deletion.
  1. +2 −1 lib/sinatra/base.rb
  2. +30 −0 test/mapped_error_test.rb
View
@@ -772,7 +772,8 @@ def error_block!(key)
next base = base.superclass unless args = base.errors[key]
return process_route(*args)
end
- false
+ return false unless key.respond_to? :superclass and key.superclass < Exception
+ error_block! key.superclass
end
def dump_errors!(boom)
View
@@ -39,6 +39,36 @@ def test_default
assert_equal 'Exception!', body
end
+ it 'walks down inheritance chain for errors' do
+ mock_app {
+ set :raise_errors, false
+ error(RuntimeError) { 'Exception!' }
+ get '/' do
+ raise FooError
+ end
+ }
+
+ get '/'
+ assert_equal 500, status
+ assert_equal 'Exception!', body
+ end
+
+ it 'favors subclass handler over superclass handler if available' do
+ mock_app {
+ set :raise_errors, false
+ error(Exception) { 'Exception!' }
+ error(FooError) { 'FooError!' }
+ error(RuntimeError) { 'Exception!' }
+ get '/' do
+ raise FooError
+ end
+ }
+
+ get '/'
+ assert_equal 500, status
+ assert_equal 'FooError!', body
+ end
+
it "sets env['sinatra.error'] to the rescued exception" do
mock_app {
set :raise_errors, false

0 comments on commit 2e0030f

Please sign in to comment.