2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2008-02-23 20:00:04 -05:00
|
|
|
|
2009-01-30 20:27:17 -05:00
|
|
|
class FooError < RuntimeError
|
|
|
|
end
|
|
|
|
|
|
|
|
class FooNotFound < Sinatra::NotFound
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2011-06-10 07:21:37 -04:00
|
|
|
class FooSpecialError < RuntimeError
|
2011-12-31 06:46:41 -05:00
|
|
|
def http_status; 501 end
|
2011-06-10 07:21:37 -04:00
|
|
|
end
|
|
|
|
|
2011-12-31 06:46:41 -05:00
|
|
|
class FooStatusOutOfRangeError < RuntimeError
|
|
|
|
def code; 4000 end
|
|
|
|
end
|
|
|
|
|
2012-05-13 15:06:54 -04:00
|
|
|
class FooWithCode < RuntimeError
|
|
|
|
def code; 419 end
|
|
|
|
end
|
|
|
|
|
2012-04-25 21:09:09 -04:00
|
|
|
class FirstError < RuntimeError; end
|
|
|
|
class SecondError < RuntimeError; end
|
2011-12-31 06:46:41 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class MappedErrorTest < Test::Unit::TestCase
|
2009-05-16 23:36:04 -04:00
|
|
|
def test_default
|
|
|
|
assert true
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'Exception Mappings' do
|
|
|
|
it 'invokes handlers registered with ::error when raised' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-03-26 11:42:13 -04:00
|
|
|
set :raise_errors, false
|
|
|
|
error(FooError) { 'Foo!' }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { raise FooError }
|
|
|
|
end
|
2009-03-26 11:42:13 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
assert_equal 'Foo!', body
|
|
|
|
end
|
|
|
|
|
2011-07-24 07:45:42 -04:00
|
|
|
it 'passes the exception object to the error handler' do
|
|
|
|
mock_app do
|
|
|
|
set :raise_errors, false
|
|
|
|
error(FooError) { |e| assert_equal(FooError, e.class) }
|
|
|
|
get('/') { raise FooError }
|
|
|
|
end
|
|
|
|
get('/')
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'uses the Exception handler if no matching handler found' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-03-26 11:42:13 -04:00
|
|
|
set :raise_errors, false
|
|
|
|
error(Exception) { 'Exception!' }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { raise FooError }
|
|
|
|
end
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
assert_equal 'Exception!', body
|
|
|
|
end
|
|
|
|
|
2011-06-04 14:07:57 -04:00
|
|
|
it 'walks down inheritance chain for errors' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2011-06-04 14:07:57 -04:00
|
|
|
set :raise_errors, false
|
|
|
|
error(RuntimeError) { 'Exception!' }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { raise FooError }
|
|
|
|
end
|
2011-06-04 14:07:57 -04:00
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
assert_equal 'Exception!', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'favors subclass handler over superclass handler if available' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2011-06-04 14:07:57 -04:00
|
|
|
set :raise_errors, false
|
|
|
|
error(Exception) { 'Exception!' }
|
|
|
|
error(FooError) { 'FooError!' }
|
|
|
|
error(RuntimeError) { 'Exception!' }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { raise FooError }
|
|
|
|
end
|
2011-06-04 14:07:57 -04:00
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
assert_equal 'FooError!', body
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it "sets env['sinatra.error'] to the rescued exception" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-03-26 11:42:13 -04:00
|
|
|
set :raise_errors, false
|
2012-05-21 17:21:59 -04:00
|
|
|
error(FooError) do
|
2009-03-26 11:42:13 -04:00
|
|
|
assert env.include?('sinatra.error')
|
|
|
|
assert env['sinatra.error'].kind_of?(FooError)
|
|
|
|
'looks good'
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { raise FooError }
|
|
|
|
end
|
2009-03-26 11:42:13 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 'looks good', body
|
|
|
|
end
|
|
|
|
|
2010-03-04 08:30:54 -05:00
|
|
|
it "raises errors from the app when raise_errors set and no handler defined" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-03-26 11:42:13 -04:00
|
|
|
set :raise_errors, true
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { raise FooError }
|
|
|
|
end
|
2009-03-26 11:42:13 -04:00
|
|
|
assert_raise(FooError) { get '/' }
|
|
|
|
end
|
|
|
|
|
2010-03-04 08:30:54 -05:00
|
|
|
it "calls error handlers before raising errors even when raise_errors is set" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2010-03-04 08:30:54 -05:00
|
|
|
set :raise_errors, true
|
|
|
|
error(FooError) { "she's there." }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { raise FooError }
|
|
|
|
end
|
2010-03-04 08:30:54 -05:00
|
|
|
assert_nothing_raised { get '/' }
|
|
|
|
assert_equal 500, status
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it "never raises Sinatra::NotFound beyond the application" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app(Sinatra::Application) do
|
|
|
|
get('/') { raise Sinatra::NotFound }
|
|
|
|
end
|
2009-03-26 11:42:13 -04:00
|
|
|
assert_nothing_raised { get '/' }
|
|
|
|
assert_equal 404, status
|
|
|
|
end
|
|
|
|
|
|
|
|
it "cascades for subclasses of Sinatra::NotFound" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-03-26 11:42:13 -04:00
|
|
|
set :raise_errors, true
|
|
|
|
error(FooNotFound) { "foo! not found." }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { raise FooNotFound }
|
|
|
|
end
|
2009-03-26 11:42:13 -04:00
|
|
|
assert_nothing_raised { get '/' }
|
|
|
|
assert_equal 404, status
|
|
|
|
assert_equal 'foo! not found.', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a not_found method for backwards compatibility' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app { not_found { "Lost, are we?" } }
|
2008-08-31 03:39:26 -04:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
get '/test'
|
|
|
|
assert_equal 404, status
|
|
|
|
assert_equal "Lost, are we?", body
|
|
|
|
end
|
2009-03-26 12:02:40 -04:00
|
|
|
|
|
|
|
it 'inherits error mappings from base class' do
|
|
|
|
base = Class.new(Sinatra::Base)
|
|
|
|
base.error(FooError) { 'base class' }
|
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app(base) do
|
2009-03-26 12:02:40 -04:00
|
|
|
set :raise_errors, false
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { raise FooError }
|
|
|
|
end
|
2009-03-26 12:02:40 -04:00
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert_equal 'base class', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'overrides error mappings in base class' do
|
|
|
|
base = Class.new(Sinatra::Base)
|
|
|
|
base.error(FooError) { 'base class' }
|
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app(base) do
|
2009-03-26 12:02:40 -04:00
|
|
|
set :raise_errors, false
|
|
|
|
error(FooError) { 'subclass' }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { raise FooError }
|
|
|
|
end
|
2009-03-26 12:02:40 -04:00
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert_equal 'subclass', body
|
|
|
|
end
|
2011-06-10 07:21:37 -04:00
|
|
|
|
2011-12-31 06:46:41 -05:00
|
|
|
it 'honors Exception#http_status if present' do
|
2011-06-10 07:21:37 -04:00
|
|
|
mock_app do
|
|
|
|
set :raise_errors, false
|
|
|
|
error(501) { 'Foo!' }
|
|
|
|
get('/') { raise FooSpecialError }
|
|
|
|
end
|
|
|
|
get '/'
|
|
|
|
assert_equal 501, status
|
|
|
|
assert_equal 'Foo!', body
|
|
|
|
end
|
2011-12-31 06:46:41 -05:00
|
|
|
|
2012-05-13 15:06:54 -04:00
|
|
|
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
|
|
|
|
|
2011-12-31 06:46:41 -05:00
|
|
|
it 'does not rely on Exception#code for invalid codes' do
|
|
|
|
mock_app do
|
|
|
|
set :raise_errors, false
|
2012-05-13 15:06:54 -04:00
|
|
|
set :use_code, true
|
2011-12-31 06:46:41 -05:00
|
|
|
get('/') { raise FooStatusOutOfRangeError }
|
|
|
|
end
|
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
end
|
|
|
|
|
2012-04-25 21:09:09 -04:00
|
|
|
it "allows a stack of exception_handlers" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2012-04-25 21:09:09 -04:00
|
|
|
set :raise_errors, false
|
|
|
|
error(FirstError) { 'First!' }
|
|
|
|
error(SecondError) { 'Second!' }
|
|
|
|
get('/'){ raise SecondError }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2012-04-25 21:09:09 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
assert_equal 'Second!', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows an exception handler to pass control to the next exception handler" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2012-04-25 21:09:09 -04:00
|
|
|
set :raise_errors, false
|
|
|
|
error(500, FirstError) { 'First!' }
|
|
|
|
error(500, SecondError) { pass }
|
|
|
|
get('/') { raise 500 }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2012-04-25 21:09:09 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
assert_equal 'First!', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows an exception handler to handle the exception" do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2012-04-25 21:09:09 -04:00
|
|
|
set :raise_errors, false
|
|
|
|
error(500, FirstError) { 'First!' }
|
|
|
|
error(500, SecondError) { 'Second!' }
|
|
|
|
get('/') { raise 500 }
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2012-04-25 21:09:09 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
assert_equal 'Second!', body
|
|
|
|
end
|
2008-09-07 07:07:45 -04:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'Custom Error Pages' do
|
|
|
|
it 'allows numeric status code mappings to be registered with ::error' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-03-26 11:42:13 -04:00
|
|
|
set :raise_errors, false
|
|
|
|
error(500) { 'Foo!' }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { [500, {}, 'Internal Foo Error'] }
|
|
|
|
end
|
2009-03-26 11:42:13 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
assert_equal 'Foo!', body
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows ranges of status code mappings to be registered with :error' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-03-26 11:42:13 -04:00
|
|
|
set :raise_errors, false
|
|
|
|
error(500..550) { "Error: #{response.status}" }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { [507, {}, 'A very special error'] }
|
|
|
|
end
|
2009-03-26 11:42:13 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 507, status
|
|
|
|
assert_equal 'Error: 507', body
|
|
|
|
end
|
|
|
|
|
2011-06-05 03:40:20 -04:00
|
|
|
it 'allows passing more than one range' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2011-06-05 03:40:20 -04:00
|
|
|
set :raise_errors, false
|
|
|
|
error(409..411, 503..509) { "Error: #{response.status}" }
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { [507, {}, 'A very special error'] }
|
|
|
|
end
|
2011-06-05 03:40:20 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 507, status
|
|
|
|
assert_equal 'Error: 507', body
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class FooError < RuntimeError
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs after exception mappings and overwrites body' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-03-26 11:42:13 -04:00
|
|
|
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' }
|
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { raise FooError }
|
|
|
|
end
|
2009-03-26 11:42:13 -04:00
|
|
|
get '/'
|
|
|
|
assert_equal 502, status
|
|
|
|
assert_equal 'from custom error page', body
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2008-02-23 20:00:04 -05:00
|
|
|
end
|