Nov
11
2005

Error Messages in a RESTful API

I haven’t seen this suggested anywhere so I thought I would just put this out there and let the lazyweb inform me who invented this first. :-)

Cool frameworks like Rails, Django , and Plone provide a nice little HTML framework to show you validation errors on screen. They pin-point exactly where you goofed. This is nice.

Here’s an example using Django : Example of validation error in Django

Now let’s say I’m writing a RESTful web services API. Let’s say I’m implementing the Atom Publishing Protocol or maybe a clone of the 37Signals’ Backpack API.

From what I can tell from these protocols, there is no guidance on how to provide “fine grained” detail on exactly where the error is. If I’m submitting a 10 item expense report, how do can I tell the user that the project code for expense item #8 is invalid and have a computer be able to interpret that and highlight it in the UI. The UI could be an excel spreadsheet or even a hacked version of a TiddlyWiki. The point is I’m trying to decouple the user interface from the server interface.

Here’s my idea… Add the error message as an attribute to the item that failed the validation.

For example, here’s an expense report that I’ll submit to the server:

Expense report in XML

The server would send the data back to the user, modified with the error text “built-in”:

By setting the error message as an attribute of the element that failed a validation, my UI code can parse the XML and will know exactly which element to highlight on screen with the error.

Of course, this assumes that the source document itself was valid XML. Anyway, that’s my idea. Thanks for reading.

posted in django, python, rails by Jason Huggins

2 Comments to "Error Messages in a RESTful API"

  1. Nat wrote:

    This would require the error attribute to be included in the schema or DTD of every validatable XML file sent back. Also, the message cannot contain rich information such as links. It might be better to define an error namespace and allow some subset of XHTML or similar markup to be used in the error message, like:

    <expense-item xmlns:error=”http://www.jrandolph.com/xmlns/error/1,0″>
    <project><error:report>Project not a <a href=”valid_codes.html”>valid code</a></error>

  2. Josh wrote:

    Or just don’t use a RESTful API. Nobody wants to bind to it anyway :)

 
Powered by Wordpress and MySQL. Theme by openark.org