<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Use whiny_finder and RecordNotFound to turn 500 into 404</title>
	<atom:link href="http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/</link>
	<description>Late nights eventually pay off</description>
	<lastBuildDate>Sun, 05 Sep 2010 16:59:36 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
	<item>
		<title>By: Adam</title>
		<link>http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/#comment-2619</link>
		<dc:creator>Adam</dc:creator>
		<pubDate>Tue, 03 Mar 2009 19:29:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-force-404-instead-of-500-responses/#comment-2619</guid>
		<description>Thanks for the idea.  I am not lazy :P so I can check for nil in my controller and raise a RecordNotFound exception.  Thanks!!!!!</description>
		<content:encoded><![CDATA[<p>Thanks for the idea.  I am not lazy <img src='http://blog.aisleten.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  so I can check for nil in my controller and raise a RecordNotFound exception.  Thanks!!!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dazza</title>
		<link>http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/#comment-2181</link>
		<dc:creator>dazza</dc:creator>
		<pubDate>Sun, 13 Apr 2008 05:25:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-force-404-instead-of-500-responses/#comment-2181</guid>
		<description>here is the easiest solution I have found for catching RecordNotFound using rescue_from

http://almosteffortless.com/2007/10/08/graceful-404s-in-rails-20/</description>
		<content:encoded><![CDATA[<p>here is the easiest solution I have found for catching RecordNotFound using rescue_from</p>
<p><a href="http://almosteffortless.com/2007/10/08/graceful-404s-in-rails-20/" rel="nofollow">http://almosteffortless.com/2007/10/08/graceful-404s-in-rails-20/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Micah</title>
		<link>http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/#comment-192</link>
		<dc:creator>Micah</dc:creator>
		<pubDate>Thu, 14 Jun 2007 21:48:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-force-404-instead-of-500-responses/#comment-192</guid>
		<description>@Tony,

Hmm, this is strange.  I was 100% positive that the standard behavior of Rails in the production environment was to return a 404 on a RecordNotFound.  I was under the impression that was the whole idea behind whiny_finder (which I didn&#039;t write).

This backs me up:
http://shifteleven.com/articles/2006/12/03/trusting-in-exceptions-to-do-what-they-do

However, when I looked at the source for rescue_action_in_public here: http://api.rubyonrails.org/classes/ActionController/Rescue.html there is no mention of RecordNotFound.  It handles RoutingError and UnknownAction exceptions, but no mention of RecordNotFound.

I guess the behavior I am seeing is a mixture of whiny_finder and exception_notification (available here: http://dev.rubyonrails.org/svn/rails/plugins/exception_notification/ ).  The exception_notification plugin (heavily recommended) adds RecordNotFound as an exception that generates a 404.

Looking at Edge Rails trunk ( http://dev.rubyonrails.org/svn/rails/trunk/actionpack/lib/action_controller/rescue.rb at rev 7026) it seems as though Edge Rails has RecordNotFound as an automatic 404, although I didn&#039;t read too deeply.  I&#039;m guessing that&#039;s why exception_notification was updated to have the same behavior.

I will update the article to mention exception_notification, Edge Rails, and/or how to update rescue_action_in_public to translate RecordNotFound into a 404.  Doing any of those should result the desired behavior.

I guess it take 15 minutes instead of 10 ;)

Thanks for the catch, Tony.</description>
		<content:encoded><![CDATA[<p>@Tony,</p>
<p>Hmm, this is strange.  I was 100% positive that the standard behavior of Rails in the production environment was to return a 404 on a RecordNotFound.  I was under the impression that was the whole idea behind whiny_finder (which I didn&#8217;t write).</p>
<p>This backs me up:<br />
<a href="http://shifteleven.com/articles/2006/12/03/trusting-in-exceptions-to-do-what-they-do" rel="nofollow">http://shifteleven.com/articles/2006/12/03/trusting-in-exceptions-to-do-what-they-do</a></p>
<p>However, when I looked at the source for rescue_action_in_public here: <a href="http://api.rubyonrails.org/classes/ActionController/Rescue.html" rel="nofollow">http://api.rubyonrails.org/classes/ActionController/Rescue.html</a> there is no mention of RecordNotFound.  It handles RoutingError and UnknownAction exceptions, but no mention of RecordNotFound.</p>
<p>I guess the behavior I am seeing is a mixture of whiny_finder and exception_notification (available here: <a href="http://dev.rubyonrails.org/svn/rails/plugins/exception_notification/" rel="nofollow">http://dev.rubyonrails.org/svn/rails/plugins/exception_notification/</a> ).  The exception_notification plugin (heavily recommended) adds RecordNotFound as an exception that generates a 404.</p>
<p>Looking at Edge Rails trunk ( <a href="http://dev.rubyonrails.org/svn/rails/trunk/actionpack/lib/action_controller/rescue.rb" rel="nofollow">http://dev.rubyonrails.org/svn/rails/trunk/actionpack/lib/action_controller/rescue.rb</a> at rev 7026) it seems as though Edge Rails has RecordNotFound as an automatic 404, although I didn&#8217;t read too deeply.  I&#8217;m guessing that&#8217;s why exception_notification was updated to have the same behavior.</p>
<p>I will update the article to mention exception_notification, Edge Rails, and/or how to update rescue_action_in_public to translate RecordNotFound into a 404.  Doing any of those should result the desired behavior.</p>
<p>I guess it take 15 minutes instead of 10 <img src='http://blog.aisleten.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Thanks for the catch, Tony.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony</title>
		<link>http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/#comment-191</link>
		<dc:creator>Tony</dc:creator>
		<pubDate>Thu, 14 Jun 2007 21:18:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-force-404-instead-of-500-responses/#comment-191</guid>
		<description>ops, I should have pointed out that, from your first paragraph, &quot;By default, Rails is set up to translate an unhandled ActiveRecord::RecordNotFound exception into a 404 response to the client&quot; is wrong.

ActiveRecord::RecordNotFound will return a 500 response to the client. The same exception and response that whiny_finder returns.

I actually take back my arguments against whiny_finder (I got a chance to read through it&#039;s code) -- you never get to 404s. In fact, it&#039;s used to raise more 500s on exceptional &quot;searches&quot; ;)</description>
		<content:encoded><![CDATA[<p>ops, I should have pointed out that, from your first paragraph, &#8220;By default, Rails is set up to translate an unhandled ActiveRecord::RecordNotFound exception into a 404 response to the client&#8221; is wrong.</p>
<p>ActiveRecord::RecordNotFound will return a 500 response to the client. The same exception and response that whiny_finder returns.</p>
<p>I actually take back my arguments against whiny_finder (I got a chance to read through it&#8217;s code) &#8212; you never get to 404s. In fact, it&#8217;s used to raise more 500s on exceptional &#8220;searches&#8221; <img src='http://blog.aisleten.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Micah</title>
		<link>http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/#comment-190</link>
		<dc:creator>Micah</dc:creator>
		<pubDate>Thu, 14 Jun 2007 20:56:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-force-404-instead-of-500-responses/#comment-190</guid>
		<description>@Tony,

Can you please explain what you mean by &quot;raise a 500 exception and reroute to 404&quot;?  I&#039;m not clear on what a 500 exception is.  I know what a 500 response code is, but the whole point of the article is to convert 500s to 404s (where appropriate...)

Assuming you mean &quot;raise an exception that would generate a 500&quot;, then I would have to disagree, as it seems overly complicated.  In most cases you would raise the exception, then immediately catch it, then reroute to a 404.  If you&#039;re vehemently opposed to whiny_finder and RecordNotFound, then just use the one-liner you posted earlier.

Also, can you please post a link to the Rails Docs you&#039;re referencing regarding the proper usage for find?  I&#039;d like to read this.  The only stuff I&#039;ve read on find() is from the RDoc for ActiveRecord::Base (located here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000992 ), and I can&#039;t find anything specific about when to use find() vs find(:first) vs find_by_().</description>
		<content:encoded><![CDATA[<p>@Tony,</p>
<p>Can you please explain what you mean by &#8220;raise a 500 exception and reroute to 404&#8243;?  I&#8217;m not clear on what a 500 exception is.  I know what a 500 response code is, but the whole point of the article is to convert 500s to 404s (where appropriate&#8230;)</p>
<p>Assuming you mean &#8220;raise an exception that would generate a 500&#8243;, then I would have to disagree, as it seems overly complicated.  In most cases you would raise the exception, then immediately catch it, then reroute to a 404.  If you&#8217;re vehemently opposed to whiny_finder and RecordNotFound, then just use the one-liner you posted earlier.</p>
<p>Also, can you please post a link to the Rails Docs you&#8217;re referencing regarding the proper usage for find?  I&#8217;d like to read this.  The only stuff I&#8217;ve read on find() is from the RDoc for ActiveRecord::Base (located here: <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000992" rel="nofollow">http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000992</a> ), and I can&#8217;t find anything specific about when to use find() vs find(:first) vs find_by_().</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony</title>
		<link>http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/#comment-187</link>
		<dc:creator>Tony</dc:creator>
		<pubDate>Thu, 14 Jun 2007 19:58:58 +0000</pubDate>
		<guid isPermaLink="false">http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-force-404-instead-of-500-responses/#comment-187</guid>
		<description>I&#039;d argue that it&#039;s a &quot;search&quot;, although I do agree that 404 is appropriate here (the search was not found).

My stance is that you raise a 500 exception and reroute to 404.

If you&#039;re rewriting all find_by_ to find_by_! (bang) anyways, why not skip the plugin and follow Rails Doc&#039;s on the proper usage of find vs find(:first) ? Sure, it&#039;s a few more characters.. but you&#039;d be using proper Rails, *and* actually logging all your real 500s.</description>
		<content:encoded><![CDATA[<p>I&#8217;d argue that it&#8217;s a &#8220;search&#8221;, although I do agree that 404 is appropriate here (the search was not found).</p>
<p>My stance is that you raise a 500 exception and reroute to 404.</p>
<p>If you&#8217;re rewriting all find_by_ to find_by_! (bang) anyways, why not skip the plugin and follow Rails Doc&#8217;s on the proper usage of find vs find(:first) ? Sure, it&#8217;s a few more characters.. but you&#8217;d be using proper Rails, *and* actually logging all your real 500s.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Micah</title>
		<link>http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/#comment-186</link>
		<dc:creator>Micah</dc:creator>
		<pubDate>Thu, 14 Jun 2007 19:47:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-force-404-instead-of-500-responses/#comment-186</guid>
		<description>@Tony,

The following link is an example from Obsidian Portal:
http://www.obsidianportal.com/campaign/kensing

&quot;kensing&quot; is the slug for a campaign, and it&#039;s not technically the ID.  Still, for all intents and purposes, it is an ID because it&#039;s guaranteed unique.

So, in the controller for this we have:
Campaign.find_by_slug(params[:slug])

Anyways, if a user writes this down and hands it off to a friend as:
http://www.obsidianportal.com/campaign/kesning  (note the typo in the slug) then the URL they created effectively requests a non-existent id.  It&#039;s not a search, but simply a request for something that&#039;s not there.  If they post this URL on the web somewhere then both humans and bots will find it and try to follow it.  I wish I were smart enough to write something that recognizes the typo and takes them to the right place, but for now I&#039;ll settle for the 404.

By using whiny_finder, I can just use find_by_slug! (bang) and it will automatically result in a 404 if the slug is not found.  The one-liner you wrote will work as well, but it has to be done every time you do a similar find.  whiny_finder effectively collapses that particular one-liner into the find() method itself.  For places where it&#039;s inappropriate, just use the regular, non-bang find().</description>
		<content:encoded><![CDATA[<p>@Tony,</p>
<p>The following link is an example from Obsidian Portal:<br />
<a href="http://www.obsidianportal.com/campaign/kensing" rel="nofollow">http://www.obsidianportal.com/campaign/kensing</a></p>
<p>&#8220;kensing&#8221; is the slug for a campaign, and it&#8217;s not technically the ID.  Still, for all intents and purposes, it is an ID because it&#8217;s guaranteed unique.</p>
<p>So, in the controller for this we have:<br />
Campaign.find_by_slug(params[:slug])</p>
<p>Anyways, if a user writes this down and hands it off to a friend as:<br />
<a href="http://www.obsidianportal.com/campaign/kesning" rel="nofollow">http://www.obsidianportal.com/campaign/kesning</a>  (note the typo in the slug) then the URL they created effectively requests a non-existent id.  It&#8217;s not a search, but simply a request for something that&#8217;s not there.  If they post this URL on the web somewhere then both humans and bots will find it and try to follow it.  I wish I were smart enough to write something that recognizes the typo and takes them to the right place, but for now I&#8217;ll settle for the 404.</p>
<p>By using whiny_finder, I can just use find_by_slug! (bang) and it will automatically result in a 404 if the slug is not found.  The one-liner you wrote will work as well, but it has to be done every time you do a similar find.  whiny_finder effectively collapses that particular one-liner into the find() method itself.  For places where it&#8217;s inappropriate, just use the regular, non-bang find().</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony</title>
		<link>http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/#comment-185</link>
		<dc:creator>Tony</dc:creator>
		<pubDate>Thu, 14 Jun 2007 19:26:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-force-404-instead-of-500-responses/#comment-185</guid>
		<description>Yes, if the user request is not found, you should check for nil and return 404. Fairly simple one liner.

render :file =&gt; &quot;public/404.html&quot;, :status =&gt; 404 and return if @result.nil?

The case here is _not_ a 404 -- it&#039;s an exception due to corruption in data. How&#039;d you manage to pass a non-existent id to find in the first place?

Robots don&#039;t enter obscure ids, just follow your internal links. Users do enter arbitrary data... but then it&#039;s a search, and should be treated as such.

Anyways, raising an exception on a user search that resulted in no matches will hide all the real 500s that you probably should be fixing.</description>
		<content:encoded><![CDATA[<p>Yes, if the user request is not found, you should check for nil and return 404. Fairly simple one liner.</p>
<p>render :file =&gt; &#8220;public/404.html&#8221;, :status =&gt; 404 and return if @result.nil?</p>
<p>The case here is _not_ a 404 &#8212; it&#8217;s an exception due to corruption in data. How&#8217;d you manage to pass a non-existent id to find in the first place?</p>
<p>Robots don&#8217;t enter obscure ids, just follow your internal links. Users do enter arbitrary data&#8230; but then it&#8217;s a search, and should be treated as such.</p>
<p>Anyways, raising an exception on a user search that resulted in no matches will hide all the real 500s that you probably should be fixing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Micah</title>
		<link>http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/#comment-184</link>
		<dc:creator>Micah</dc:creator>
		<pubDate>Thu, 14 Jun 2007 18:32:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-force-404-instead-of-500-responses/#comment-184</guid>
		<description>@Tony,

Assuming you&#039;re checking for nil on user input, what happens when you search for something and get nil?  What should you do then?  You could redirect them to a page warning them that it wasn&#039;t found.  But wait, isn&#039;t that what a 404 page is for?

Or, you can use whiny_finder to do the checking for you and throw the RecordNotFound exception, which automatically takes them to the 404 page.  The end result is pretty much the same.

I don&#039;t recommend masking 500s as 404s, but the case being dealt with here _is_ a 404.  It only becomes a 500 if you don&#039;t (or forget to) do any checking on the value returned from find() and then try to execute an operation on it.</description>
		<content:encoded><![CDATA[<p>@Tony,</p>
<p>Assuming you&#8217;re checking for nil on user input, what happens when you search for something and get nil?  What should you do then?  You could redirect them to a page warning them that it wasn&#8217;t found.  But wait, isn&#8217;t that what a 404 page is for?</p>
<p>Or, you can use whiny_finder to do the checking for you and throw the RecordNotFound exception, which automatically takes them to the 404 page.  The end result is pretty much the same.</p>
<p>I don&#8217;t recommend masking 500s as 404s, but the case being dealt with here _is_ a 404.  It only becomes a 500 if you don&#8217;t (or forget to) do any checking on the value returned from find() and then try to execute an operation on it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony</title>
		<link>http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-turn-500-into-404/#comment-183</link>
		<dc:creator>Tony</dc:creator>
		<pubDate>Thu, 14 Jun 2007 18:21:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.aisleten.com/2007/06/13/use-whiny_finder-and-recordnotfound-to-force-404-instead-of-500-responses/#comment-183</guid>
		<description>When you call Model.find(id), you are passing it an exact id, that you presumably have gotten somewhere else, internally. Such as from another record. So when an exact record of a given id is expected, but not found - it&#039;s an exceptional situation and that calls for a RecordNotFound exception to be thrown.

If you are handling user input, you can&#039;t be sure that valid information is supplied, so you search for it.

Model.find(:first, :conditions =&gt; ... )

You either get the first record that matches, or nil. Which is alright, because you check for such kind of stuff, right? ;)

Masking your 500s with 404s is like saying &quot;I write _really_ sloppy code. And actively ignore it!&quot;</description>
		<content:encoded><![CDATA[<p>When you call Model.find(id), you are passing it an exact id, that you presumably have gotten somewhere else, internally. Such as from another record. So when an exact record of a given id is expected, but not found &#8211; it&#8217;s an exceptional situation and that calls for a RecordNotFound exception to be thrown.</p>
<p>If you are handling user input, you can&#8217;t be sure that valid information is supplied, so you search for it.</p>
<p>Model.find(:first, :conditions =&gt; &#8230; )</p>
<p>You either get the first record that matches, or nil. Which is alright, because you check for such kind of stuff, right? <img src='http://blog.aisleten.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Masking your 500s with 404s is like saying &#8220;I write _really_ sloppy code. And actively ignore it!&#8221;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
