honor Exception#code if present

This commit is contained in:
Konstantin Haase 2011-06-10 13:21:37 +02:00
parent a984c719fa
commit d8fc41c822
2 changed files with 22 additions and 9 deletions

View File

@ -791,17 +791,15 @@ module Sinatra
raise boom if settings.show_exceptions? and settings.show_exceptions != :after_handler raise boom if settings.show_exceptions? and settings.show_exceptions != :after_handler
@response.status = boom.respond_to?(:code) ? Integer(boom.code) : 500 @response.status = boom.respond_to?(:code) ? Integer(boom.code) : 500
if @response.status == 404 if not_found?
@response.headers['X-Cascade'] = 'pass' headers['X-Cascade'] = 'pass'
@response.body = ['<h1>Not Found</h1>'] body '<h1>Not Found</h1>'
end end
if res = error_block!(boom.class) res = error_block!(boom.class) || error_block!(status)
res return res if res or not server_error?
elsif @response.status >= 500 raise boom if settings.raise_errors? or settings.show_exceptions?
raise boom if settings.raise_errors? or settings.show_exceptions? error_block! Exception
error_block! Exception
end
end end
# Find an custom error block for the key(s) specified. # Find an custom error block for the key(s) specified.

View File

@ -6,6 +6,10 @@ end
class FooNotFound < Sinatra::NotFound class FooNotFound < Sinatra::NotFound
end end
class FooSpecialError < RuntimeError
def code; 501 end
end
class MappedErrorTest < Test::Unit::TestCase class MappedErrorTest < Test::Unit::TestCase
def test_default def test_default
assert true assert true
@ -173,6 +177,17 @@ class MappedErrorTest < Test::Unit::TestCase
get '/' get '/'
assert_equal 'subclass', body assert_equal 'subclass', body
end end
it 'honors Exception#code if present' do
mock_app do
set :raise_errors, false
error(501) { 'Foo!' }
get('/') { raise FooSpecialError }
end
get '/'
assert_equal 501, status
assert_equal 'Foo!', body
end
end end
describe 'Custom Error Pages' do describe 'Custom Error Pages' do