I was playing around with ActiveResource from the edge rails revision
6032, and had a nightmare trying to get returned errors from my
RESTful controller.
After a long search, I found out in here: http://dev.rubyonrails.org/
browser/trunk/activeresource/test/base_errors_test.rb?rev=6032
on line 7 that you supposed to return http status code 422 along with
your error xml!!
so suppose you have code like this:
format.xml { render :xml => @user.errors.to_xml }
active resource will die horribly!!
The correct way is format.xml { render :xml =>
@user.errors.to_xml, :status => 422 }
I was curious about what is this mysterious status code, so I searched
in google, wikipedia, etc...
Could find it!! What the heck does 422 mean?!
Also found some change logs here documenting this:
http://dev.rubyonrails.org/browser/trunk/activeresource/CHANGELOG?rev=6032
But I'm still unsatisfied about it! Did some one in rails developement
team just came up with his own error code?
Hopefully that's not the case...
Anyone knows what does 422 mean?
on 2007-02-16 09:05
on 2007-02-16 09:35
On 2/16/07, aq1018 <aq1018@gmail.com> wrote: > so suppose you have code like this: > > Anyone knows what does 422 mean? http://www.zvon.org/tmRFC/RFC2518/Output/chapter10.html#sub3 The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous XML instructions. format.xml { render :xml => @user.errors.to_xml, :status => :unprocessable_entity } -- Rick Olson http://weblog.techno-weenie.net http://mephistoblog.com
on 2007-02-16 09:48
Hi,
Here is more information about the 422 Unprocessable Entity
status code:
http://dev.rubyonrails.org/ticket/7097
Its also in the IANA HTTP Status Code registry:
http://www.iana.org/assignments/http-status-codes
In this case WebDAV defined a status code to deal with the
case where a request contains information that can be parsed
but is contains invalid information.
Its considered good practice to reuse registered status codes
(even those from RFC's other than 2616) rather than inventing
your own, provided they fit your needs. In this case 422 is
a perfect fit when you want to tell the client that the data
they submitted was parsed, but found to be invalid.
BTW: in your code I'd probably do this instead:
format.xml { render :xml => @user.errors.to_xml, :status
=> :unprocessable_entity }
Most people don't remember what all the status codes mean,
and the symbol names are a bit more descriptive and easier
to remember.
Dan
On 16-Feb-07, at 12:04 AM, aq1018 wrote:
>
> Could find it!! What the heck does 422 mean?!
> Anyone knows what does 422 mean?
__________________________________________________________________
Dan Kubb
Autopilot Marketing Inc.
Email: dan.kubb@autopilotmarketing.com
Phone: 1 (604) 820-0212
Web: http://autopilotmarketing.com/
vCard: http://autopilotmarketing.com/~dan.kubb/vcard
__________________________________________________________________
on 2007-02-16 19:39
So, wait.... we're supposed to always return 422 for a REST request that
has errors?
Or, as the OP said:
<snip>
so suppose you have code like this:
format.xml { render :xml => @user.errors.to_xml }
active resource will die horribly!!
The correct way is format.xml { render :xml =>
@user.errors.to_xml, :status => 422 }
</snip>
Never mind the numeric code versus the names (I have just as hard a time
remembering the names as the numerics, by the way)... it sounds like
scaffold_resource isn't doing the full job for us. (I've given up on
to_xml too... it never gives me enough control).
b
on 2007-02-17 12:12
Thanks for the information, guys, that helped me out alot.
I didn't want to use :status => 422 either, it's somewhat ugly...
Yes, I think for ActiveResource to properly hand error messages, the
RESTful controller needs to send :status => :unprocessable_entity
whenever you use model.errors.to_xml. Example:
def create
@person = Person.new(params[:person])
respond_to do |format|
if @person.save
format.html { redirect_to person_path(@person) }
format.xml { head :created, :location => person_url(@person) }
else
format.html { render :action => 'new' }
format.xml { render :xml => @person.errors.to_xml :status
=> :unprocessable_entity }
end
end
on 2007-02-17 12:15
Errr... mistake in the code...
this is the right one... (missed the "end" )
def create
@person = Person.new(params[:person])
respond_to do |format|
if @person.save
format.html { redirect_to person_path(@person) }
format.xml { head :created, :location => person_url(@person) }
else
format.html { render :action => 'new' }
format.xml { render :xml => @person.errors.to_xml :status
=> :unprocessable_entity }
end
end
end
on 2007-02-19 08:08
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi Ben, > So, wait.... we're supposed to always return 422 for a REST request > that > has errors? The short answer to your question is: Yes, when a model is invalid the best status code to return is 422. More broadly, there are many different reasons to return an error, 422 is only useful for one specific kind. Whenever the client sends something in the request the server likes you can return any of the 4xx status codes... It could be anything from submitting a request to a URL that doesn't exist (404) or has been explicitly deleted (410). Maybe the URL is too long to be handled (414) or maybe the method isn't allowed (405). Maybe the request can be parsed, but the data you get after parsing is invalid (422). There's more in the IANA status code registry but you get the idea: http://www.iana.org/assignments/http-status-codes Even though a well designed client will treat any 4xx status that it doesn't understand as 400, I still wouldn't use 400 unless there was no other 4xx status code in the registry that matches. When you use a more specific error status code you give the client (browser, bot, etc) a better indication of what went wrong, improving the odds that it can self-recover without human intervention. > (I've given up on to_xml too... it never gives me enough control). The scaffold_resource should probably be updated to use 422, but I agree: I don't think to_xml like the above example is too useful except in the uncommon case (for me) where I only have a single model object in an action. - -- Thanks, Dan __________________________________________________________________ Dan Kubb Autopilot Marketing Inc. Email: dan.kubb@autopilotmarketing.com Phone: 1 (604) 820-0212 Web: http://autopilotmarketing.com/ vCard: http://autopilotmarketing.com/~dan.kubb/vcard __________________________________________________________________ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (Darwin) iD8DBQFF2Uzv4DfZD7OEWk0RAuUtAJ9E9XceMoPxAnrIeB3BmfM+LZ+wkACdHfeN skPYab+ru/Yk4jQoLWJtcYY= =fPhM -----END PGP SIGNATURE-----
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.