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

Already on GitHub? Sign in to your account

Add Missing Keys from Journey on Failed URL Format #7230

Merged
merged 2 commits into from Aug 29, 2012

Conversation

Projects
None yet
6 participants
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

Owner

rafaelfranca commented Aug 1, 2012

Cool. 👍

Member

steveklabnik commented Aug 1, 2012

I like it! 👍

Owner

pixeltrix commented Aug 1, 2012

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

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

Looks nice! Thanks @schneems.

Owner

rafaelfranca commented Aug 25, 2012

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

Owner

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

Owner

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

Member

schneems commented Aug 28, 2012

@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/rails@5e55f91 ATP actionpack & railties.

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

Owner

rafaelfranca commented Aug 28, 2012

Seems good. Now it needs a rebase.

schneems added some commits Aug 1, 2012

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

schneems commented Aug 29, 2012

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

pixeltrix added a commit that referenced this pull request Aug 29, 2012

Merge pull request #7230 from schneems/schneems/expose_required_keys
Add Missing Keys from Journey on Failed URL Format

@pixeltrix pixeltrix merged commit 46d8543 into rails:master Aug 29, 2012

Owner

pixeltrix commented Aug 29, 2012

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

Member

jonleighton commented Aug 29, 2012

great job 👍

Member

schneems commented Aug 29, 2012

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