allow passing more than one argument to #error

This commit is contained in:
Konstantin Haase 2011-06-05 09:40:20 +02:00
parent e9e3c8500a
commit b391e7f1fe
2 changed files with 18 additions and 3 deletions

View File

@ -876,9 +876,11 @@ module Sinatra
# 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) { ... }`

View File

@ -202,6 +202,19 @@ class MappedErrorTest < Test::Unit::TestCase
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