<?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>a work on process &#187; Face-Saving</title>
	<atom:link href="http://jystewart.net/process/tag/Face-Saving/feed/" rel="self" type="application/rss+xml" />
	<link>http://jystewart.net/process</link>
	<description>notes from another web developer</description>
	<lastBuildDate>Sat, 21 Aug 2010 13:09:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Trapping errors in partials without bringing down the page</title>
		<link>http://jystewart.net/process/2007/04/trapping-errors-in-partials-without-bringing-down-the-page/</link>
		<comments>http://jystewart.net/process/2007/04/trapping-errors-in-partials-without-bringing-down-the-page/#comments</comments>
		<pubDate>Fri, 13 Apr 2007 21:40:53 +0000</pubDate>
		<dc:creator>James Stewart</dc:creator>
				<category><![CDATA[Notes]]></category>
		<category><![CDATA[ActionView]]></category>
		<category><![CDATA[Error Handling]]></category>
		<category><![CDATA[Exceptions]]></category>
		<category><![CDATA[Face-Saving]]></category>
		<category><![CDATA[Partials]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://jystewart.net/process/2007/04/trapping-errors-in-partials-without-bringing-down-the-page/</guid>
		<description><![CDATA[Within a large project that uses a lot of partials to load in page components, there are a lot of potential points of failure. Add in a developer new to the project, or a front-end developer pressed into service working directly on your views, and the chances of someone editing a partial without realising it&#8217;s <a href="http://jystewart.net/process/2007/04/trapping-errors-in-partials-without-bringing-down-the-page/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>Within a large project that uses a lot of partials to load in page components, there are a lot of potential points of failure. Add in a developer new to the project, or a front-end developer pressed into service working directly on your views, and the chances of someone editing a partial without realising it&#8217;s used in several more places than they&#8217;d noticed are fairly high. And if the partial hits an error, the exception will roll all the way up the chain and the end-user will see your 500 page.</p>
<p>Now there are several things that should prevent this from being a problem. Your test coverage should be high enough and run often enough that a page throwing an exception will be noticed very quickly. The partials should be self-contained enough that it doesn&#8217;t really matter what context they&#8217;re called in. And the project should be well enough documented that a developer can quickly see what dependencies any given piece of code might have.</p>
<p>Every now and again that&#8217;s still not enough, or time doesn&#8217;t allow you to be quite as consistent as you might want. I&#8217;ve run into this particular problem a couple of times, and have just added a solution to one project that I hope will give us a little more protection should the worst happen and a problematic partial makes it into the live environment.</p>
<p>By adding the following code in a file that is loaded from config/environments/production.rb I&#8217;m able to trap any exceptions that might occur in a partial, log them, and ensure the worst that happens to a page is a blank space that should have contained content:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActionView
  <span style="color:#9966CC; font-weight:bold;">module</span> Partials
&nbsp;
    private
    <span style="color:#9966CC; font-weight:bold;">def</span> render_and_rescue_partial<span style="color:#006600; font-weight:bold;">&#40;</span>partial_path, local_assigns = <span style="color:#0000FF; font-weight:bold;">nil</span>, deprecated_local_assigns = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#:nodoc:</span>
      unrescued_render_partial<span style="color:#006600; font-weight:bold;">&#40;</span>partial_path, local_assigns, deprecated_local_assigns<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> err
      <span style="color:#008000; font-style:italic;"># This is where we handle the exception, logging it, emailing, or whatever</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#996600;">''</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    alias_method <span style="color:#ff3333; font-weight:bold;">:unrescued_render_partial</span>, <span style="color:#ff3333; font-weight:bold;">:render_partial</span>
    alias_method <span style="color:#ff3333; font-weight:bold;">:render_partial</span>, <span style="color:#ff3333; font-weight:bold;">:render_and_rescue_partial</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Basically, it wraps itself around the standard render_partial method, captures any exceptions, handles them, and then returns an empty string. It wouldn&#8217;t be hard to extend it to be a little more refined and only capture errors from a certain subset of contexts.</p>
<p>This sort of error handling is a bad idea in a development environment, where you want to see errors as they happen to ensure you&#8217;re motivated to fix them. In production, this might help us look a little less foolish if something slips through the net, and let the user see the page they requested.</p>
]]></content:encoded>
			<wfw:commentRss>http://jystewart.net/process/2007/04/trapping-errors-in-partials-without-bringing-down-the-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
