<?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>Floorplanner Tech Blog &#187; http_status_exceptions</title>
	<atom:link href="http://techblog.floorplanner.com/tag/http_status_exceptions/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.floorplanner.com</link>
	<description>Our latest geek adventures!</description>
	<lastBuildDate>Tue, 16 Mar 2010 18:45:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>HTTP status exception handling plugin</title>
		<link>http://techblog.floorplanner.com/2008/09/21/http-status-exception-handling-plugin/</link>
		<comments>http://techblog.floorplanner.com/2008/09/21/http-status-exception-handling-plugin/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 11:01:16 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[http status codes]]></category>
		<category><![CDATA[http_status_exceptions]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=210</guid>
		<description><![CDATA[Some time ago, I wrote about putting HTTP status code to use for your Rails application. For my reinvigorated project, I wanted to apply the same technique. Instead of re-implementing it once again, I created a Rails plugin called http_status_exceptions to easily add this functionality and I have put it on Github. For more information [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago, I wrote about <a href="http://techblog.floorplanner.com/2008/03/11/putting-http-status-codes-to-use-with-rails/">putting HTTP status code to use</a> for your Rails application. For my reinvigorated project, I wanted to apply the same technique. Instead of re-implementing it once again, I created a Rails plugin called <strong>http_status_exceptions</strong> to easily add this functionality and I have put it on <a href="http://github.com/wvanbergen/http_status_exceptions/">Github</a>. For more information on how to install and use the plugin, see the <a href="http://github.com/wvanbergen/http_status_exceptions/wikis">project&#8217;s wiki</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2008/09/21/http-status-exception-handling-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Putting HTTP status codes to use with Rails</title>
		<link>http://techblog.floorplanner.com/2008/03/11/putting-http-status-codes-to-use-with-rails/</link>
		<comments>http://techblog.floorplanner.com/2008/03/11/putting-http-status-codes-to-use-with-rails/#comments</comments>
		<pubDate>Tue, 11 Mar 2008 19:40:57 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[HTTP status]]></category>
		<category><![CDATA[http_status_exceptions]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.suite75.net/blog/dev/putting-http-status-codes-to-use-with-rails.html</guid>
		<description><![CDATA[
Note: I have released a Rails plugin that creates an exception class for every HTTP status to your Rails application and adds a default handler for these exceptions, based on the examples in this post.

We are currently implementing an API for Floorplanner, so other sites can use the service Floorplanner offers for their own needs. [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em><br />
<strong>Note:</strong> I have <a href="http://wiki.github.com/wvanbergen/http_status_exceptions">released a Rails plugin</a> that creates an exception class for every HTTP status to your Rails application and adds a default handler for these exceptions, based on the examples in this post.<br />
</em></p></blockquote>
<p>We are currently implementing an API for Floorplanner, so other sites can use the service Floorplanner offers for their own needs. The Floorplanner website is developed with Rails, so we are trying to be a good Rails citizen and create a API based on <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">REST</a>.</p>
<p>REST embraces the HTTP protocol by coupling URIs to resources and HTTP methods <code>GET</code>, <code>PUT</code>, <code>POST</code> and <code>DELETE</code>) to actions for manipulating them. Today, I tried to embrace the HTTP protocol even more by using its various <a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes">status codes</a> for Floorplanner&#8217;s particular needs.</p>
<p>Most people will know the HTTP 404 status code, which a server returns if you request a page that does not exist. But there are many more interesting codes that can be put to good use as well. The <em>Forbidden</em> status code (403) can for instance be returned if you try to access a another user&#8217;s floorplan. Moreover, Floorplanner offers paid subscriptions that include additional functionality. If you try to access this functionality with an account without the necessary privileges, an <em>Upgrade Required</em> status code (426) can be returned. And if you forgot to pay your subscription fee, a <em>Payment Required</em> code (402) can be returned to indicate this.</p>
<p>However, in the end, users wants to see a nice and informative page that tells them what is wrong without cryptic error codes. This is easily possible by leveraging some new features in Rails 2. First of all, we define some custom exceptions that can be raised if some of these conditions occur is wrong:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> PermissionDenied <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;">StandardError</span>
  <span style="color:#008000; font-style:italic;"># no further implementation necessary</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> AccountExpired <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;">StandardError</span>
  <span style="color:#008000; font-style:italic;"># no further implementation necessary</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, we can raise these exceptions when needed in our controllers:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> show
  <span style="color:#0066ff; font-weight:bold;">@plan</span> = Floorplan.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">raise</span> PermissionDenied <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#0066ff; font-weight:bold;">@plan</span>.<span style="color:#9900CC;">user</span> == current_user
  <span style="color:#008000; font-style:italic;"># ...</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> login
  <span style="color:#008000; font-style:italic;"># ...</span>
  <span style="color:#CC0066; font-weight:bold;">raise</span> AccountExpired <span style="color:#9966CC; font-weight:bold;">if</span> current_user.<span style="color:#9900CC;">account_expired</span>?
  <span style="color:#008000; font-style:italic;"># ...</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Normally, raised exceptions will trigger an Internal server error (HTTP status code 500). The new exceptions will be handled manually to return the intended status code and serve a pretty page explaining what is wrong. Exceptions can be caught using the <code>rescue_from</code> method. If we put the code in the Application controller, it will automagically work for all our controllers. DRY.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ApplicationController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
&nbsp;
  rescue_from PermissionDenied <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> http_status_code<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:forbidden</span>, e<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  rescue_from AccountExpired   <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> http_status_code<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:payment_required</span>, e<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Returns a HTTP status code, with a nice error page</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> http_status_code<span style="color:#006600; font-weight:bold;">&#40;</span>status, exception<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># store the exception so its message can be used in the view</span>
    <span style="color:#0066ff; font-weight:bold;">@exception</span> = exception
&nbsp;
    <span style="color:#008000; font-style:italic;"># Only add the error page to the status code if the reuqest-format was HTML</span>
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:template</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;shared/status_#{status.to_s}&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> status <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">any</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> head status <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#008000; font-style:italic;"># only return the status code</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, it is easy to create super fancy pages by editing the views file like <code>app/views/shared/status_forbidden.html.erb</code>.</p>
<p>Note that you cannot and should not use this method for handling internal server errors with status code 500. These should not occur because we have thought of every possible way our code will be used and misused&#8230; in theory. In practice, we installed the <a href="http://agilewebdevelopment.com/plugins/exception_notifier">exception notifier plugin</a>, so we receive a message if one of these occurs and get our asses back to work.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2008/03/11/putting-http-status-codes-to-use-with-rails/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
