Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow passing more than one argument to #error

  • Loading branch information...
commit b391e7f1fe6c0304ce15d675511eaf312b8caf34 1 parent e9e3c85
Konstantin Haase rkh authored
Showing with 18 additions and 3 deletions.
  1. +5 −3 lib/sinatra/base.rb
  2. +13 −0 test/mapped_error_test.rb
8 lib/sinatra/base.rb
View
@@ -876,9 +876,11 @@ def disable(*opts)
# Define a custom error handler. Optionally takes either an Exception
# class, or an HTTP status code to specify which errors should be
# handled.
- def error(codes = Exception, &block)
- args = compile! "ERROR", //, block
- Array(codes).each { |c| @errors[c] = args }
+ def error(*codes, &block)
+ args = compile! "ERROR", //, block
+ codes = codes.map { |c| Array(c) }.flatten
+ codes << Exception if codes.empty?
+ codes.each { |c| @errors[c] = args }
end
# Sugar for `error(404) { ... }`
13 test/mapped_error_test.rb
View
@@ -202,6 +202,19 @@ def test_default
assert_equal 'Error: 507', body
end
+ it 'allows passing more than one range' do
+ mock_app {
+ set :raise_errors, false
+ error(409..411, 503..509) { "Error: #{response.status}" }
+ get '/' do
+ [507, {}, 'A very special error']
+ end
+ }
+ get '/'
+ assert_equal 507, status
+ assert_equal 'Error: 507', body
+ end
+
class FooError < RuntimeError
end
Please sign in to comment.
Something went wrong with that request. Please try again.