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

View File

@ -6,6 +6,10 @@ end
class FooNotFound < Sinatra::NotFound
end
class FooSpecialError < RuntimeError
def code; 501 end
end
class MappedErrorTest < Test::Unit::TestCase
def test_default
assert true
@ -173,6 +177,17 @@ class MappedErrorTest < Test::Unit::TestCase
get '/'
assert_equal 'subclass', body
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
describe 'Custom Error Pages' do