Add Missing Keys from Journey on Failed URL Format #7230

Merged
merged 2 commits into from Aug 29, 2012

Projects

None yet

6 participants

@schneems
Member
schneems commented Aug 1, 2012

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
Member

Cool. 👍

@steveklabnik
Member

I like it! 👍

@pixeltrix
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
Member
schneems commented Aug 1, 2012

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

Looks nice! Thanks @schneems.

@rafaelfranca
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
rafaelfranca Aug 25, 2012 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
pixeltrix Aug 27, 2012 Ruby on Rails member

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

@schneems
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
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
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
Member

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

@jonleighton
Member

great job 👍

@schneems
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