Skip to content

Commit

Permalink
Show full route constraints in error message
Browse files Browse the repository at this point in the history
When an optimized helper fails to generate, show the full route constraints
in the error message. Previously it would only show the contraints that were
required as part of the path.

Fixes #13592
  • Loading branch information
pixeltrix committed Jan 5, 2014
1 parent b9efc74 commit 892c539
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
10 changes: 10 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,3 +1,13 @@
* Show full route constraints in error message

When an optimized helper fails to generate, show the full route constraints
in the error message. Previously it would only show the contraints that were
required as part of the path.

Fixes #13592

*Andrew White*

* Use a custom route visitor for optimized url generation. Fixes #13349. * Use a custom route visitor for optimized url generation. Fixes #13349.


*Andrew White* *Andrew White*
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_dispatch/journey/formatter.rb
Expand Up @@ -33,8 +33,8 @@ def generate(type, name, options, recall = {}, parameterize = nil)
return [route.format(parameterized_parts), params] return [route.format(parameterized_parts), params]
end end


message = "No route matches #{constraints.inspect}" message = "No route matches #{Hash[constraints.sort].inspect}"
message << " missing required keys: #{missing_keys.inspect}" if name message << " missing required keys: #{missing_keys.sort.inspect}" if name


raise ActionController::UrlGenerationError, message raise ActionController::UrlGenerationError, message
end end
Expand Down
5 changes: 3 additions & 2 deletions actionpack/lib/action_dispatch/routing/route_set.rb
Expand Up @@ -210,8 +210,9 @@ def missing_keys(args)
end end


def raise_generation_error(args, missing_keys) def raise_generation_error(args, missing_keys)
message = "No route matches #{args.inspect}" constraints = Hash[@route.requirements.merge(args).sort]
message << " missing required keys: #{missing_keys.inspect}" message = "No route matches #{constraints.inspect}"
message << " missing required keys: #{missing_keys.sort.inspect}"


raise ActionController::UrlGenerationError, message raise ActionController::UrlGenerationError, message
end end
Expand Down
25 changes: 25 additions & 0 deletions actionpack/test/dispatch/routing_test.rb
Expand Up @@ -3708,3 +3708,28 @@ def test_redirect_doesnt_match_unnamed_route
end end
end end
end end

class TestUrlGenerationErrors < ActionDispatch::IntegrationTest
Routes = ActionDispatch::Routing::RouteSet.new.tap do |app|
app.draw do
get "/products/:id" => 'products#show', :as => :product
end
end

def app; Routes end

include Routes.url_helpers

test "url helpers raise a helpful error message whem generation fails" do
url, missing = { action: 'show', controller: 'products', id: nil }, [:id]
message = "No route matches #{url.inspect} missing required keys: #{missing.inspect}"

# Optimized url helper
error = assert_raises(ActionController::UrlGenerationError){ product_path(nil) }
assert_equal message, error.message

# Non-optimized url helper
error = assert_raises(ActionController::UrlGenerationError, message){ product_path(id: nil) }
assert_equal message, error.message
end
end

0 comments on commit 892c539

Please sign in to comment.