Add Missing Keys from Journey on Failed URL Format #7230

Merged
merged 2 commits into from Aug 29, 2012

6 participants

@schneems
Ruby on Rails member

Many named routes have keys that are required to successfully resolve. If a key is left off like this:

<%= link_to 'user', user_path %>

An error will be raised like this:

No route matches {:action=>"show", :controller=>"users"}

Since Journey know's that the :id is missing, we can add extra debugging information to the error message.

No route matches {:action=>"show", :controller=>"users"} missing required keys: [:id]

This will help new and seasoned developers look closer at their parameters. I've also subclassed the routing error to be clear that this error is a result of attempting to generate a url and not because the user is trying to visit a bad url.

The current error message is misleading and confuses most developers. The important part isn't what's in the options, the important part is that we are missing keys. Adding this information to the error message will make debugging much more obvious.

This is the sister pull request of rails/journey#44 which will be required to get they missing keys into the correct error message. Opening both issues at the same time to start a dialog.

Example Development Error in Rails: http://cl.ly/image/3S0T0n1T3421

cc/ @pixeltrix

@rafaelfranca
Ruby on Rails member

Cool. 👍

@steveklabnik
Ruby on Rails member

I like it! 👍

@pixeltrix
Ruby on Rails member

@schneems can you add a test to make sure that raising an ActionController::UrlGenerationError returns a 500 response code and not a 404 - thanks!

@schneems
Ruby on Rails member

Added a test, let me know if there is a better place, or if you want more: https://github.com/rails/rails/pull/7230/files#L3R118

@carlosantoniodasilva
Ruby on Rails member

Looks nice! Thanks @schneems.

@rafaelfranca
Ruby on Rails member

Could you rebase this one, add a CHANGELOG and update all the related docs? I'm going to merge it since rails/journey#44 was accepted.

@rafaelfranca rafaelfranca and 1 other commented on an outdated diff Aug 25, 2012
actionpack/lib/action_dispatch/routing/route_set.rb
end
- def raise_routing_error
- raise ActionController::RoutingError, "No route matches #{options.inspect}"
+ def raise_routing_error(message = "")
+ raise ActionController::UrlGenerationError, "No route matches #{options.inspect} #{message}"
@rafaelfranca
Ruby on Rails member

This is something that bother me, but if we don't have message we still have a trailing space after the options.

@pixeltrix
Ruby on Rails member

Is raise_routing_error ever called without a message? Does the message argument need to be optional?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@schneems
Ruby on Rails member

@pixeltrix @rafaelfranca I also refactored some of the code so we can get rid of the error method. This gets rid of the whitespace problem and cleans up the interface to the generator class :) If you're not fans, I can revert. schneems@5e55f91 ATP actionpack & railties.

I checked the guides and greped around, didn't see any docs containing this error message. Added a changelog entry.

@rafaelfranca
Ruby on Rails member

Seems good. Now it needs a rebase.

schneems added some commits Aug 1, 2012
@schneems schneems Add Missing Keys from Journey on failed URL format
Many named routes have keys that are required to successfully resolve. If a key is left off like this:

    <%= link_to 'user', user_path %>

This will produce an error like this:

    No route matches {:action=>"show", :controller=>"users"}

Since we know that the :id is missing, we can add extra debugging information to the error message.

    No route matches {:action=>"show", :controller=>"users"} missing required keys: [:id]


This will help new and seasoned developers look closer at their parameters. I've also subclassed the routing error to be clear that this error is a result of attempting to generate a url and not because the user is trying to visit a bad url. 

While this may sound trivial this error message is misleading and confuses most developers. The important part isn't what's in the options its's what's missing. Adding this information to the error message will make debugging much more obvious. 

This is the sister pull request of rails/journey#44 which will be required to get they missing keys into the correct error message. 

Example Development Error in Rails: http://cl.ly/image/3S0T0n1T3421
0b6175a
@schneems schneems refactor route_set `generate_extras` functionality
The result of Generator with or without the @extras instance variable set contains the desired information. Rather than preserving state when initializing the original object, we can simply extract the keys from the resultant parameters.

ATP Actionpack, railties
cc57b2a
@schneems
Ruby on Rails member

Code has been rebased, ready for merge. Let me know if I need to do anything else.

@pixeltrix pixeltrix merged commit 46d8543 into rails:master Aug 29, 2012
@pixeltrix
Ruby on Rails member

Thanks for your work on this @schneems - hopefully it should reduce the number of false reports we get about routes not matching.

@jonleighton
Ruby on Rails member

great job 👍

@schneems
Ruby on Rails member

Thanks for all the eyes and all the help, you all rock!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment