REST - HTTP headers - XML Error Handling -ActiveResource


I’m putting together a RESTful API that I would like to play nice with
ActiveResource. I’ve got things mostly working, but I’m beginning to
stumble around error handling and I’m seeking some thoughtful input.

Here’s some context:

(For a detailed look at the issue I’m contemplating see:
O'Reilly Media - Technology and Business Training)

Let’s say that I have a REST controller containing code that looks like

def create
@book = @book(params[:book])
respond_to do |format|
if false
format.xml { head :created, :location => book_url(@book) }
format.xml { head 400, :xml=>, “Save
failed.”).to_xml }

(note, I’m deliberately forcing a failure response with ‘if false’ )
When I execute this method via CURL, I get following (correct, I
believe) response:

t$ curl -i -X POST
HTTP/1.1 400 Bad Request
Connection: close
Date: Wed, 11 Apr 2007 19:46:11 GMT
Set-Cookie: _adiserver_session_id=dbe7f61d877a041c3d5d49c29d0086e5;
Status: 400 Bad Request
Xml: <?xml version="1.0" encoding="UTF-8"?>

Could not save book
save failed.


This response looks correct me because it has:

  1. an HTTP Status code
  2. some descriptive XML

Seeing this, I expect that ActiveResource should be happy…

but, alas, when doing a over ActiveResource (console), I get:

ActiveResource::ResourceNotFound: Failed with 404
activeresource/lib/active_resource/connection.rb:92:in `handle_response’


Now I would expect to get ‘false’ and/or the the XML containing the
error information that I’ve constructed.


My questions are these:

  1. What does ActiveResource expect in the case of failed operation?
  2. In the event of a failed request, how does AR get access to the
    supplemental XML that is sent? Is this possible?
  3. Are some of my assumptions incorrect?

Perhaps “ActiveResource::ResourceNotFound” is an exception that I need
to handle in the following manner:

[…could not save code here …]

I look forward to your thoughtful guidance.