Permalink
Browse files

let use_code default to false, remove warning. fixes #497

  • Loading branch information...
1 parent 6ec5234 commit 97e1bd2c807f5a9a7bdd97788fa71b2fe3714463 @rkh rkh committed May 13, 2012
Showing with 28 additions and 7 deletions.
  1. +3 −3 CHANGES
  2. +1 −4 lib/sinatra/base.rb
  3. +24 −0 test/mapped_error_test.rb
View
@@ -10,9 +10,9 @@
* The `provides` condition now respects an earlier set content type.
(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)
+ * Exception#code is only used when :use_code is enabled. Moreover, it will
+ be ignored if the value is not between 400 and 599. You should use
+ Exception#http_status instead. (Konstantin Haase)
* Status, headers and body will be set correctly in an after filter when using
halt in a before filter or route. (Konstantin Haase)
View
@@ -960,9 +960,6 @@ def handle_exception!(boom)
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
@@ -1588,7 +1585,7 @@ def self.force_encoding(data, *) data end
set :logging, false
set :protection, true
set :method_override, false
- set :use_code, true
+ set :use_code, false
set :default_encoding, "utf-8"
set :add_charset, %w[javascript xml xhtml+xml json].map { |t| "application/#{t}" }
settings.add_charset << /^text\//
View
@@ -14,6 +14,10 @@ class FooStatusOutOfRangeError < RuntimeError
def code; 4000 end
end
+class FooWithCode < RuntimeError
+ def code; 419 end
+end
+
class FirstError < RuntimeError; end
class SecondError < RuntimeError; end
@@ -200,9 +204,29 @@ def test_default
assert_equal 'Foo!', body
end
+ it 'does not use Exception#code by default' do
+ mock_app do
+ set :raise_errors, false
+ get('/') { raise FooWithCode }
+ end
+ get '/'
+ assert_equal 500, status
+ end
+
+ it 'uses Exception#code if use_code is enabled' do
+ mock_app do
+ set :raise_errors, false
+ set :use_code, true
+ get('/') { raise FooWithCode }
+ end
+ get '/'
+ assert_equal 419, status
+ end
+
it 'does not rely on Exception#code for invalid codes' do
mock_app do
set :raise_errors, false
+ set :use_code, true
get('/') { raise FooStatusOutOfRangeError }
end
get '/'

0 comments on commit 97e1bd2

Please sign in to comment.