aq1018
2007-Feb-16 08:04 UTC
[Rails Rev 6032] HTTP Status Code 422 for ActiveResource Errors, but what does 422 mean??
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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rick Olson
2007-Feb-16 08:34 UTC
Re: [Rails Rev 6032] HTTP Status Code 422 for ActiveResource Errors, but what does 422 mean??
On 2/16/07, aq1018 <aq1018-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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?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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dan Kubb
2007-Feb-16 08:46 UTC
Re: [Rails Rev 6032] HTTP Status Code 422 for ActiveResource Errors, but what does 422 mean??
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:> 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?__________________________________________________________________ Dan Kubb Autopilot Marketing Inc. Email: dan.kubb-HY0TxqzkUC3nxq293hCaZDKzEDxYleXD@public.gmane.org Phone: 1 (604) 820-0212 Web: http://autopilotmarketing.com/ vCard: http://autopilotmarketing.com/~dan.kubb/vcard __________________________________________________________________ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ben Munat
2007-Feb-16 18:38 UTC
Re: [Rails Rev 6032] HTTP Status Code 422 for ActiveResource Errors, but what does 422 mean??
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 Dan Kubb wrote:> 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: > >> 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? > > __________________________________________________________________ > > Dan Kubb > Autopilot Marketing Inc. > > Email: dan.kubb-HY0TxqzkUC3nxq293hCaZDKzEDxYleXD@public.gmane.org > Phone: 1 (604) 820-0212 > Web: http://autopilotmarketing.com/ > vCard: http://autopilotmarketing.com/~dan.kubb/vcard > __________________________________________________________________ > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
aq1018
2007-Feb-17 11:10 UTC
Re: [Rails Rev 6032] HTTP Status Code 422 for ActiveResource Errors, but what does 422 mean??
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 Feb 16, 10:38 am, Ben Munat <bmu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > > > Dan Kubb wrote: > > 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: > > >> 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? > > > __________________________________________________________________ > > > Dan Kubb > > Autopilot Marketing Inc. > > > Email: dan.k...-HY0TxqzkUC3nxq293hCaZDKzEDxYleXD@public.gmane.org > > Phone: 1 (604) 820-0212 > > Web: http://autopilotmarketing.com/ > > vCard:http://autopilotmarketing.com/~dan.kubb/vcard > > __________________________________________________________________- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
aq1018
2007-Feb-17 11:13 UTC
Re: HTTP Status Code 422 for ActiveResource Errors, but what does 422 mean??
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 Feb 17, 3:10 am, "aq1018" <aq1...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Feb 16, 10:38 am, Ben Munat <bmu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > 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 > > > Dan Kubb wrote: > > > 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: > > > >> 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? > > > > __________________________________________________________________ > > > > Dan Kubb > > > Autopilot Marketing Inc. > > > > Email: dan.k...-HY0TxqzkUC3nxq293hCaZDKzEDxYleXD@public.gmane.org > > > Phone: 1 (604) 820-0212 > > > Web: http://autopilotmarketing.com/ > > > vCard:http://autopilotmarketing.com/~dan.kubb/vcard > > > __________________________________________________________________- Hide quoted text - > > > - Show quoted text -- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dan Kubb
2007-Feb-19 07:08 UTC
Re: [Rails Rev 6032] HTTP Status Code 422 for ActiveResource Errors, but what does 422 mean??
-----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.>>> 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 } > > 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).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-HY0TxqzkUC3nxq293hCaZDKzEDxYleXD@public.gmane.org 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----- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---