Skip to content

Commit

Permalink
Merge pull request #997 from ujifgc/master
Browse files Browse the repository at this point in the history
Improving exception handling for filters
  • Loading branch information
Darío Javier Cravero committed Jan 10, 2013
2 parents 153d49d + 43c9ace commit e9b14c0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
12 changes: 7 additions & 5 deletions padrino-core/lib/padrino-core/application/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -907,13 +907,15 @@ def filter!(type, base=settings)
def dispatch!
static! if settings.static? && (request.get? || request.head?)
route!
rescue ::Sinatra::NotFound => boom
filter! :before
handle_exception!(boom)
rescue ::Exception => boom
handle_exception!(boom)
filter! :before if boom.kind_of? ::Sinatra::NotFound
@boom_handled = handle_exception!(boom)
ensure
filter! :after unless env['sinatra.static_file']
@boom_handled or begin
filter! :after unless env['sinatra.static_file']
rescue ::Exception => boom
handle_exception!(boom)
end
end

ROUTE_NOT_FOUND_STATUS = 9404
Expand Down
51 changes: 51 additions & 0 deletions padrino-core/test/test_filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,55 @@
assert_equal 'before', once
end

should 'catch exceptions in before filters' do
doodle = nil
mock_app do
after do
doodle = 'Been after'
end
before do
raise StandardError, "before"
end
get :index do
doodle = 'Been now'
end
error 500 do
"We broke #{env['sinatra.error'].message}"
end
end

get '/'
assert_equal 'We broke before', body
assert_equal nil, doodle
end

should 'catch exceptions in after filters if no exceptions caught before' do
doodle = ''
mock_app do
after do
doodle += ' and after'
raise StandardError, "after"
end
get :foo do
doodle = 'Been now'
raise StandardError, "now"
end
get :index do
doodle = 'Been now'
end
error 500 do
"We broke #{env['sinatra.error'].message}"
end
end

get '/foo'
assert_equal 'We broke now', body
assert_equal 'Been now', doodle

doodle = ''
get '/'
assert_equal 'We broke after', body
assert_equal 'Been now and after', doodle
end

end

0 comments on commit e9b14c0

Please sign in to comment.