Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make route params available during error handler (Fixes #860) #895

Merged
merged 1 commit into from
Jan 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,9 @@ def process_route(pattern, keys, conditions, block = nil, values = [])
conditions.each { |c| throw :pass if c.bind(self).call == false }
block ? block[self, values] : yield(self, values)
end
rescue
@env['sinatra.error.params'] = @params
raise
ensure
@params = original if original
end
Expand Down Expand Up @@ -1088,6 +1091,9 @@ def dispatch!

# Error handling during requests.
def handle_exception!(boom)
if error_params = @env['sinatra.error.params']
@params = @params.merge(error_params)
end
@env['sinatra.error'] = boom

if boom.respond_to? :http_status
Expand Down
17 changes: 17 additions & 0 deletions test/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,23 @@ class RoutingTest < Test::Unit::TestCase
assert_equal 'Hello World', body
end

it "makes original request params available in error handler" do
mock_app {
disable :raise_errors

get '/:foo' do
raise ArgumentError, "foo"
end

error do
"Hello #{params['foo']}2"
end
}

get '/bar'
assert_equal 'Hello bar2', body
end

it "transitions to 404 when passed and no subsequent route matches" do
mock_app {
get '/:foo' do
Expand Down