<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>xambr::blog &#187; handling 404</title>
	<atom:link href="http://blog.xambr.com/tag/handling-404/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xambr.com</link>
	<description>crafting my thoughts</description>
	<lastBuildDate>Tue, 20 Jul 2010 18:57:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Ramaze Error Handling</title>
		<link>http://blog.xambr.com/2010/07/19/ramaze-error-handling/</link>
		<comments>http://blog.xambr.com/2010/07/19/ramaze-error-handling/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 02:39:37 +0000</pubDate>
		<dc:creator>max.rb</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[handling 404]]></category>
		<category><![CDATA[ramaze]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.xambr.com/?p=281</guid>
		<description><![CDATA[I recently had to build custom 404 &#38; exception handlers for a Ramaze application. There are several useful discussions on the Ramaze Google Group regarding this topic but I thought I&#8217;d synthesize the solution here for the sake of convenience. Handling 404 Errors Ramaze has a built-in controller action to handle 404 errors; it presents a [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to build custom 404 &amp; exception handlers for a Ramaze application. There are several useful discussions on the <a href="http://groups.google.com/group/ramaze" target="_blank">Ramaze Google Group</a> regarding this topic but I thought I&#8217;d synthesize the solution here for the sake of convenience.</p>
<h2>Handling 404 Errors</h2>
<p><a href="http://ramaze.net/" target="_blank">Ramaze</a> has a built-in controller action to handle 404 errors; it presents a vanilla page stating &#8216;No action found at: <code>/xyz</code>&#8216;. Overriding this action is easy and <a href="http://doc.rubyists.com/ramaze%2binnate/Innate/Node.html#action_missing-instance_method" target="_blank">well documented</a> but there are a couple of things that might want to consider.</p>
<p>First, if you would like all 404 errors across your application to be handled by a single action, you will need to add your handling code to the base controller.</p>
<p>Second, using a view file corresponding to the controller action does not quite work. While the view is correctly rendered when the 404 occurs directly under a path mapped to the 404 handler&#8217;s controller (eg. <code>/my_missing_action</code>) it is not rendered for 404 errors occurring under paths mapped to other controllers (eg. <code>/users/my_missing_action</code>).</p>
<p>A workaround for this is to use the <code><a href="http://doc.rubyists.com/ramaze%2binnate/Innate/Helper/Render.html#render_file-instance_method" target="_blank">render_file</a></code> method to manually render a view you&#8217;ve created. I have not done due diligence on this problem so I cannot tell you why it occurs &#8211; maybe it is a bug or maybe (likely) I&#8217;m missing something. If anyone has an explanation or a better work around I&#8217;d love to hear it.</p>
<h3>Source for a Custom 404 Handler</h3>
<p><script src="http://gist.github.com/482339.js"> </script></p>
<h2>Handling Exceptions</h2>
<h3>Routing Exceptions</h3>
<p><a href="http://ramaze.net/" target="_blank">Ramaze</a> also has a built-in exception handler which presents a developer-friendly stack trace (one which I&#8217;m sure you will want to hide for the end-users). The <a href="http://groups.google.com/group/ramaze" target="_blank">Ramaze Google Group</a> discussions <a href="http://groups.google.com/group/ramaze/browse_thread/thread/7d90b3f24b80d01e/c92b7eaae3ab73b3?lnk=gst&amp;q=exception+handling#c92b7eaae3ab73b3" target="_blank">here</a> and <a href="http://groups.google.com/group/ramaze/browse_thread/thread/66f7246c29b1a20c/8d0fc5300ebdc34f?lnk=gst&amp;q=exception+handling#8d0fc5300ebdc34f" target="_blank">here</a> talk about how to override this default handler. It boils down to using <code><a href="http://doc.rubyists.com/ramaze%2binnate/Rack/RouteExceptions.html#route-class_method" target="_blank">Rack::RouteExceptions.route(exception, to)</a>;</code> a method that takes the type of exception to route and the path of the action to route it to.</p>
<p>For example, to route all exceptions to the same controller you could use (as stated by Lars Olsson <a href="http://groups.google.com/group/ramaze/browse_thread/thread/7d90b3f24b80d01e/c92b7eaae3ab73b3?lnk=gst&amp;q=exception+handling#c92b7eaae3ab73b3" target="_blank">here</a>):</p>
<pre>Rack::RouteExceptions.route(Exception, MyController.r(:error))</pre>
<p>Alternatively, to handle all IOError exceptions separately you could use:</p>
<pre>Rack::RouteExceptions.route(IOError, MyController.r(:io_error))
Rack::RouteExceptions.route(Exception, MyController.r(:error))</pre>
<p>Note the order of the calls to route is important. You must route the most specific exceptions first or they will be routed to more general handlers specified before them.</p>
<p>Unlike the 404 error handler, using a standard view file <strong>does </strong>work here.</p>
<h3>The Exception Object</h3>
<p>To obtain the exception object within the controller action you can call:</p>
<pre>@error = request.env[Rack::RouteExceptions::EXCEPTION]</pre>
<h3>Source for a Custom Exception Handler</h3>
<p><script src="http://gist.github.com/482348.js"> </script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.xambr.com/2010/07/19/ramaze-error-handling/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

