<?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: rails exceptions in xml</title>
	<atom:link href="http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/</link>
	<description>Sarah Allen's reflections on internet software and other topics</description>
	<lastBuildDate>Sun, 08 Jan 2012 16:57:41 -0800</lastBuildDate>
	
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Sarah</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/comment-page-1/#comment-1105</link>
		<dc:creator>Sarah</dc:creator>
		<pubDate>Thu, 26 Nov 2009 19:32:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2058#comment-1105</guid>
		<description>That&#039;s just a regular error I think, not thrown as an exception...</description>
		<content:encoded><![CDATA[<p>That&#8217;s just a regular error I think, not thrown as an exception&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Gum</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/comment-page-1/#comment-1103</link>
		<dc:creator>Josh Gum</dc:creator>
		<pubDate>Wed, 25 Nov 2009 20:17:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2058#comment-1103</guid>
		<description>Sorry, was actually :

HTTP/1.1 422 
Connection: close
Date: Wed, 25 Nov 2009 20:16:40 GMT
Content-Type: application/xml; charset=utf-8
Cache-Control: no-cache
Content-Length: 141



  record_not_found
  Couldn&#039;t find Offer with ID=999
</description>
		<content:encoded><![CDATA[<p>Sorry, was actually :</p>
<p>HTTP/1.1 422<br />
Connection: close<br />
Date: Wed, 25 Nov 2009 20:16:40 GMT<br />
Content-Type: application/xml; charset=utf-8<br />
Cache-Control: no-cache<br />
Content-Length: 141</p>
<p>  record_not_found<br />
  Couldn&#8217;t find Offer with ID=999</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Gum</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/comment-page-1/#comment-1102</link>
		<dc:creator>Josh Gum</dc:creator>
		<pubDate>Wed, 25 Nov 2009 20:09:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2058#comment-1102</guid>
		<description>Sure.. What&#039;s strange is your respond block was rendering HTML..? I&#039;ve got the same thing setup and mine return json and xml.

curl -H &#039;Accepts: text/xml&#039; -i http://127.0.0.1:3000/offers/999.xml
HTTP/1.1 422 
Connection: close
Date: Wed, 25 Nov 2009 20:04:35 GMT
Content-Type: application/xml; charset=utf-8
Cache-Control: no-cache
Content-Length: 68

Internal Server Error Couldn&#039;t find Offer with ID=999</description>
		<content:encoded><![CDATA[<p>Sure.. What&#8217;s strange is your respond block was rendering HTML..? I&#8217;ve got the same thing setup and mine return json and xml.</p>
<p>curl -H &#8216;Accepts: text/xml&#8217; -i <a href="http://127.0.0.1:3000/offers/999.xml" rel="nofollow">http://127.0.0.1:3000/offers/999.xml</a><br />
HTTP/1.1 422<br />
Connection: close<br />
Date: Wed, 25 Nov 2009 20:04:35 GMT<br />
Content-Type: application/xml; charset=utf-8<br />
Cache-Control: no-cache<br />
Content-Length: 68</p>
<p>Internal Server Error Couldn&#8217;t find Offer with ID=999</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sarah</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/comment-page-1/#comment-1101</link>
		<dc:creator>Sarah</dc:creator>
		<pubDate>Wed, 25 Nov 2009 20:03:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2058#comment-1101</guid>
		<description>Ok, maybe that&#039;s a little more rails-y, but I&#039;m not sure that it is more readable than just plunking the wrapper xml node into a string and inserting the exception.message with string interpolation as we did.</description>
		<content:encoded><![CDATA[<p>Ok, maybe that&#8217;s a little more rails-y, but I&#8217;m not sure that it is more readable than just plunking the wrapper xml node into a string and inserting the exception.message with string interpolation as we did.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Gum</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/comment-page-1/#comment-1100</link>
		<dc:creator>Josh Gum</dc:creator>
		<pubDate>Wed, 25 Nov 2009 19:48:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2058#comment-1100</guid>
		<description>Try;

format.xml  { render :xml =&gt; {:message =&gt; &quot;Internal Server Error #{exception.message}&quot;}.to_xml(:root =&gt; &quot;error&quot;), :status =&gt; 500}

format.json { render :json =&gt; {:error =&gt; { :message =&gt;  &quot;Internal Server Error #{exception.message}&quot;}}, :status =&gt; 500</description>
		<content:encoded><![CDATA[<p>Try;</p>
<p>format.xml  { render :xml =&gt; {:message =&gt; &#8220;Internal Server Error #{exception.message}&#8221;}.to_xml(:root =&gt; &#8220;error&#8221;), :status =&gt; 500}</p>
<p>format.json { render :json =&gt; {:error =&gt; { :message =&gt;  &#8220;Internal Server Error #{exception.message}&#8221;}}, :status =&gt; 500</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wolfram Arnold</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/comment-page-1/#comment-1000</link>
		<dc:creator>Wolfram Arnold</dc:creator>
		<pubDate>Thu, 24 Sep 2009 19:12:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2058#comment-1000</guid>
		<description>I like Erik Ostrom&#039;s suggestion, but I would refine it to say that if the accept header calls for HTML Rails behaves as today, as many web sites rely on this Rails default to return an HTML error message.

Where Rails fails today is that is replies in HTML even if the accept header specifies something else (e.g. XML or JSON). In that case, the default behavior would probably be best to just return a status code and leave it to the application developer to add textual error messages.</description>
		<content:encoded><![CDATA[<p>I like Erik Ostrom&#8217;s suggestion, but I would refine it to say that if the accept header calls for HTML Rails behaves as today, as many web sites rely on this Rails default to return an HTML error message.</p>
<p>Where Rails fails today is that is replies in HTML even if the accept header specifies something else (e.g. XML or JSON). In that case, the default behavior would probably be best to just return a status code and leave it to the application developer to add textual error messages.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sarah</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/comment-page-1/#comment-982</link>
		<dc:creator>Sarah</dc:creator>
		<pubDate>Mon, 14 Sep 2009 02:27:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2058#comment-982</guid>
		<description>Good point.  I think empty content with a status code is a reasonable solution.</description>
		<content:encoded><![CDATA[<p>Good point.  I think empty content with a status code is a reasonable solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erik Ostrom</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/comment-page-1/#comment-981</link>
		<dc:creator>Erik Ostrom</dc:creator>
		<pubDate>Mon, 14 Sep 2009 02:04:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2058#comment-981</guid>
		<description>It seems reasonable that exceptions should always be returned in the format the client requests, but I&#039;m not sure I&#039;d want Rails to enforce this. Since XML is sort of a meta-format, it&#039;s not enough to just have Rails return &quot;something in XML&quot;; it has to return something that conforms to the client&#039;s expectations about how exceptions are rendered in XML. That&#039;s application-specific.

(Or at least, uh, &quot;specific-kind-of-XML-specific&quot;. Like, if you&#039;re using SOAP, that has its own exception protocol.)

Maybe when there&#039;s an unhandled exception Rails should just return an HTTP error code with no content? Unless the application sets up an exception handler?</description>
		<content:encoded><![CDATA[<p>It seems reasonable that exceptions should always be returned in the format the client requests, but I&#8217;m not sure I&#8217;d want Rails to enforce this. Since XML is sort of a meta-format, it&#8217;s not enough to just have Rails return &#8220;something in XML&#8221;; it has to return something that conforms to the client&#8217;s expectations about how exceptions are rendered in XML. That&#8217;s application-specific.</p>
<p>(Or at least, uh, &#8220;specific-kind-of-XML-specific&#8221;. Like, if you&#8217;re using SOAP, that has its own exception protocol.)</p>
<p>Maybe when there&#8217;s an unhandled exception Rails should just return an HTTP error code with no content? Unless the application sets up an exception handler?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sarah</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/comment-page-1/#comment-980</link>
		<dc:creator>Sarah</dc:creator>
		<pubDate>Sun, 13 Sep 2009 23:58:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2058#comment-980</guid>
		<description>Yeah, I consider it a bug in my code that there wasn&#039;t a validation on the model; however, I wanted to be sure if *any* code throws an exception that it is returned to the client as XML (because the client isn&#039;t built to expect HTML as a response to an XML API nor should it be).

Certainly the exception is not caught and perhaps it should be by ActiveRecord, which could then populate @person.errors with the message from the database adapter.</description>
		<content:encoded><![CDATA[<p>Yeah, I consider it a bug in my code that there wasn&#8217;t a validation on the model; however, I wanted to be sure if *any* code throws an exception that it is returned to the client as XML (because the client isn&#8217;t built to expect HTML as a response to an XML API nor should it be).</p>
<p>Certainly the exception is not caught and perhaps it should be by ActiveRecord, which could then populate @person.errors with the message from the database adapter.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Austin Putman</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/comment-page-1/#comment-979</link>
		<dc:creator>Austin Putman</dc:creator>
		<pubDate>Sun, 13 Sep 2009 23:48:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2058#comment-979</guid>
		<description>Don&#039;t know if its a bug per se.  It&#039;s odd as the database is doing validation in addition to the validations done by the model.  I would say that if the 40 character limit is important, you could put an ActiveRecord validation in to prevent this situation.  Then you will get the same error across all databases.

It surprises me that the controller logic doesn&#039;t handle this case.  Usually the database update is tested as a conditional:

if @person.save
   respond_to # happy path block
  ...
else
  respond_to # error message logic block
  ....
end

In your case, the save is throwing an error, like save!.  The AR::Postgres adapter could maybe rescue that error and return false, but how can it represent the nature of the error in a way that is accessible to the controller?</description>
		<content:encoded><![CDATA[<p>Don&#8217;t know if its a bug per se.  It&#8217;s odd as the database is doing validation in addition to the validations done by the model.  I would say that if the 40 character limit is important, you could put an ActiveRecord validation in to prevent this situation.  Then you will get the same error across all databases.</p>
<p>It surprises me that the controller logic doesn&#8217;t handle this case.  Usually the database update is tested as a conditional:</p>
<p>if @person.save<br />
   respond_to # happy path block<br />
  &#8230;<br />
else<br />
  respond_to # error message logic block<br />
  &#8230;.<br />
end</p>
<p>In your case, the save is throwing an error, like save!.  The AR::Postgres adapter could maybe rescue that error and return false, but how can it represent the nature of the error in a way that is accessible to the controller?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

