1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

setting status to 404 in error handler should not trigger not_found handler, fixes #541

This commit is contained in:
Konstantin Haase 2013-02-26 13:56:59 +11:00
parent f1b7c3a146
commit 86bc9ac094
4 changed files with 70 additions and 21 deletions

View file

@ -67,6 +67,9 @@
* Default to only serving localhost in development mode. (Postmodern) * Default to only serving localhost in development mode. (Postmodern)
* Setting status code to 404 in error handler no longer triggers not_found
handler. (Konstantin Haase)
* Improve documentation. (Kashyap, Stanislav Chistenko, Zachary Scott, * Improve documentation. (Kashyap, Stanislav Chistenko, Zachary Scott,
Anthony Accomazzo, Peter Suschlik, Rachel Mehl, ymmtmsys, Anurag Priyam, Anthony Accomazzo, Peter Suschlik, Rachel Mehl, ymmtmsys, Anurag Priyam,
burningTyger, Tony Miller, akicho8, Vasily Polovnyov, Markus Prinz, burningTyger, Tony Miller, akicho8, Vasily Polovnyov, Markus Prinz,

View file

@ -849,7 +849,7 @@ module Sinatra
@response['Content-Type'] = nil @response['Content-Type'] = nil
invoke { dispatch! } invoke { dispatch! }
invoke { error_block!(response.status) } invoke { error_block!(response.status) } unless @env['sinatra.error']
unless @response['Content-Type'] unless @response['Content-Type']
if Array === body and body[0].respond_to? :content_type if Array === body and body[0].respond_to? :content_type

View file

@ -280,6 +280,72 @@ class HelpersTest < Test::Unit::TestCase
assert_equal 'FAIL', body assert_equal 'FAIL', body
end end
it 'should not invoke error handler when setting status inside an error handler' do
mock_app do
disable :raise_errors
not_found do
body "not_found handler"
status 404
end
error do
body "error handler"
status 404
end
get '/' do
raise
end
end
get '/'
assert_equal 404, status
assert_equal 'error handler', body
end
it 'should not invoke error handler when halting with 500 inside an error handler' do
mock_app do
disable :raise_errors
not_found do
body "not_found handler"
halt 404
end
error do
body "error handler"
halt 404
end
get '/' do
raise
end
end
get '/'
assert_equal 404, status
assert_equal 'error handler', body
end
it 'should not invoke not_found handler when halting with 404 inside a not found handler' do
mock_app do
disable :raise_errors
not_found do
body "not_found handler"
halt 500
end
error do
body "error handler"
halt 500
end
end
get '/'
assert_equal 500, status
assert_equal 'not_found handler', body
end
it 'uses a 500 status code when first argument is a body' do it 'uses a 500 status code when first argument is a body' do
mock_app do mock_app do
get('/') do get('/') do

View file

@ -281,25 +281,5 @@ class MappedErrorTest < Test::Unit::TestCase
assert_equal 507, status assert_equal 507, status
assert_equal 'Error: 507', body assert_equal 'Error: 507', body
end end
class FooError < RuntimeError
end
it 'runs after exception mappings and overwrites body' do
mock_app do
set :raise_errors, false
error FooError do
response.status = 502
'from exception mapping'
end
error(500) { 'from 500 handler' }
error(502) { 'from custom error page' }
get('/') { raise FooError }
end
get '/'
assert_equal 502, status
assert_equal 'from custom error page', body
end
end end
end end