mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Fixes #673 and adds additional test for before filter
This commit is contained in:
parent
6f315b619c
commit
854c32cfa4
2 changed files with 32 additions and 7 deletions
|
@ -1053,7 +1053,11 @@ module Sinatra
|
||||||
rescue ::Exception => boom
|
rescue ::Exception => boom
|
||||||
invoke { handle_exception!(boom) }
|
invoke { handle_exception!(boom) }
|
||||||
ensure
|
ensure
|
||||||
filter! :after unless env['sinatra.static_file']
|
begin
|
||||||
|
filter! :after unless env['sinatra.static_file']
|
||||||
|
rescue ::Exception => boom
|
||||||
|
invoke { handle_exception!(boom) } unless @env['sinatra.error']
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Error handling during requests.
|
# Error handling during requests.
|
||||||
|
|
|
@ -153,6 +153,27 @@ class BeforeFilterTest < Test::Unit::TestCase
|
||||||
get '/foo/bar'
|
get '/foo/bar'
|
||||||
assert_equal subpath, 'bar'
|
assert_equal subpath, 'bar'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'can catch exceptions in before filters and handle them properly' do
|
||||||
|
doodle = ''
|
||||||
|
mock_app do
|
||||||
|
before do
|
||||||
|
doodle += 'This begins'
|
||||||
|
raise StandardError, "before"
|
||||||
|
end
|
||||||
|
get "/" do
|
||||||
|
doodle = 'and runs'
|
||||||
|
end
|
||||||
|
error 500 do
|
||||||
|
"Error handled #{env['sinatra.error'].message}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
doodle = ''
|
||||||
|
get '/'
|
||||||
|
assert_equal 'Error handled before', body
|
||||||
|
assert_equal 'This begins', doodle
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class AfterFilterTest < Test::Unit::TestCase
|
class AfterFilterTest < Test::Unit::TestCase
|
||||||
|
@ -442,25 +463,25 @@ class AfterFilterTest < Test::Unit::TestCase
|
||||||
doodle += ' and after'
|
doodle += ' and after'
|
||||||
raise StandardError, "after"
|
raise StandardError, "after"
|
||||||
end
|
end
|
||||||
get :foo do
|
get "/foo" do
|
||||||
doodle = 'Been now'
|
doodle = 'Been now'
|
||||||
raise StandardError, "now"
|
raise StandardError, "now"
|
||||||
end
|
end
|
||||||
get :index do
|
get "/" do
|
||||||
doodle = 'Been now'
|
doodle = 'Been now'
|
||||||
end
|
end
|
||||||
error 500 do
|
error 500 do
|
||||||
"We broke #{env['sinatra.error'].message}"
|
"Error handled #{env['sinatra.error'].message}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/foo'
|
get '/foo'
|
||||||
assert_equal 'We broke now', body
|
assert_equal 'Error handled now', body
|
||||||
assert_equal 'Been now', doodle
|
assert_equal 'Been now and after', doodle
|
||||||
|
|
||||||
doodle = ''
|
doodle = ''
|
||||||
get '/'
|
get '/'
|
||||||
assert_equal 'We broke after', body
|
assert_equal 'Error handled after', body
|
||||||
assert_equal 'Been now and after', doodle
|
assert_equal 'Been now and after', doodle
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue