honor Exception#code if present
This commit is contained in:
parent
a984c719fa
commit
d8fc41c822
|
@ -791,18 +791,16 @@ 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
|
||||
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
|
||||
end
|
||||
|
||||
# Find an custom error block for the key(s) specified.
|
||||
def error_block!(key)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue