mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Better error mapping inheritance
This commit is contained in:
parent
4de90276a0
commit
5c405a76d1
2 changed files with 41 additions and 5 deletions
|
@ -575,11 +575,16 @@ module Sinatra
|
|||
|
||||
# Find an custom error block for the key(s) specified.
|
||||
def error_block!(*keys)
|
||||
errmap = self.class.errors
|
||||
keys.each do |key|
|
||||
if block = errmap[key]
|
||||
res = instance_eval(&block)
|
||||
return res
|
||||
base = self.class
|
||||
while base.respond_to?(:errors)
|
||||
if block = base.errors[key]
|
||||
# found a handler, eval and return result
|
||||
res = instance_eval(&block)
|
||||
return res
|
||||
else
|
||||
base = base.superclass
|
||||
end
|
||||
end
|
||||
end
|
||||
nil
|
||||
|
@ -900,7 +905,7 @@ module Sinatra
|
|||
@templates = {}
|
||||
@conditions = []
|
||||
@filters = []
|
||||
@errors = base.errors.dup
|
||||
@errors = {}
|
||||
@middleware = base.middleware.dup
|
||||
@prototype = nil
|
||||
@extensions = []
|
||||
|
|
|
@ -101,6 +101,37 @@ class MappedErrorTest < Test::Unit::TestCase
|
|||
assert_equal 404, status
|
||||
assert_equal "Lost, are we?", body
|
||||
end
|
||||
|
||||
it 'inherits error mappings from base class' do
|
||||
base = Class.new(Sinatra::Base)
|
||||
base.error(FooError) { 'base class' }
|
||||
|
||||
mock_app(base) {
|
||||
set :raise_errors, false
|
||||
get '/' do
|
||||
raise FooError
|
||||
end
|
||||
}
|
||||
|
||||
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' }
|
||||
|
||||
mock_app(base) {
|
||||
set :raise_errors, false
|
||||
error(FooError) { 'subclass' }
|
||||
get '/' do
|
||||
raise FooError
|
||||
end
|
||||
}
|
||||
|
||||
get '/'
|
||||
assert_equal 'subclass', body
|
||||
end
|
||||
end
|
||||
|
||||
describe 'Custom Error Pages' do
|
||||
|
|
Loading…
Reference in a new issue