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

deprecate Exception#code usage, ignore values that are not between 400 and 599. fixes #423.

This commit is contained in:
Konstantin Haase 2011-12-31 12:46:41 +01:00
parent 5c07737ea9
commit 0d85006d01
3 changed files with 36 additions and 4 deletions

View file

@ -3,6 +3,10 @@
* No longer include Sinatra::Delegator in Object, instead extend the main * No longer include Sinatra::Delegator in Object, instead extend the main
object only. (Konstantin Haase) object only. (Konstantin Haase)
* Exception#code is only used when :use_code is enabled and displays a warning.
Moreover, it will be ignored if the value is not between 400 and 599. You
should use Exception#http_status instead. (Konstantin Haase)
= 1.3.2 / 2011-12-30 = 1.3.2 / 2011-12-30
* Don't automatically add `Rack::CommonLogger` if `Rack::Server` is adding it, * Don't automatically add `Rack::CommonLogger` if `Rack::Server` is adding it,

View file

@ -88,7 +88,7 @@ module Sinatra
end end
class NotFound < NameError #:nodoc: class NotFound < NameError #:nodoc:
def code ; 404 ; end def http_status; 404 end
end end
# Methods available to routes, before/after filters, and views. # Methods available to routes, before/after filters, and views.
@ -893,7 +893,19 @@ module Sinatra
# Error handling during requests. # Error handling during requests.
def handle_exception!(boom) def handle_exception!(boom)
@env['sinatra.error'] = boom @env['sinatra.error'] = boom
status boom.respond_to?(:code) ? Integer(boom.code) : 500
if boom.respond_to? :http_status
status(boom.http_status)
elsif settings.use_code? and boom.respond_to? :code and boom.code.between? 400, 599
status(boom.code)
warn "Using #{boom.class}#code (#{status}) for setting status code. This is deprecated. " \
"Use #http_status instead. If this happens unintentional, please \`disable :use_code\`" \
" in your application."
else
status(500)
end
status(500) unless status.between? 400, 599
if server_error? if server_error?
dump_errors! boom if settings.dump_errors? dump_errors! boom if settings.dump_errors?
@ -1491,6 +1503,7 @@ module Sinatra
set :logging, false set :logging, false
set :protection, true set :protection, true
set :method_override, false set :method_override, false
set :use_code, true
set :default_encoding, "utf-8" set :default_encoding, "utf-8"
set :add_charset, %w[javascript xml xhtml+xml json].map { |t| "application/#{t}" } set :add_charset, %w[javascript xml xhtml+xml json].map { |t| "application/#{t}" }
settings.add_charset << /^text\// settings.add_charset << /^text\//

View file

@ -7,9 +7,14 @@ class FooNotFound < Sinatra::NotFound
end end
class FooSpecialError < RuntimeError class FooSpecialError < RuntimeError
def code; 501 end def http_status; 501 end
end end
class FooStatusOutOfRangeError < RuntimeError
def code; 4000 end
end
class MappedErrorTest < Test::Unit::TestCase class MappedErrorTest < Test::Unit::TestCase
def test_default def test_default
assert true assert true
@ -182,7 +187,7 @@ class MappedErrorTest < Test::Unit::TestCase
assert_equal 'subclass', body assert_equal 'subclass', body
end end
it 'honors Exception#code if present' do it 'honors Exception#http_status if present' do
mock_app do mock_app do
set :raise_errors, false set :raise_errors, false
error(501) { 'Foo!' } error(501) { 'Foo!' }
@ -192,6 +197,16 @@ class MappedErrorTest < Test::Unit::TestCase
assert_equal 501, status assert_equal 501, status
assert_equal 'Foo!', body assert_equal 'Foo!', body
end end
it 'does not rely on Exception#code for invalid codes' do
mock_app do
set :raise_errors, false
get('/') { raise FooStatusOutOfRangeError }
end
get '/'
assert_equal 500, status
end
end end
describe 'Custom Error Pages' do describe 'Custom Error Pages' do