Skip to content
This repository
Browse code

Interpret 422 Unprocessable Entity as ResourceInvalid. Closes #7097.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5967 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit e00e6a29410e4ae75abc0480a4d79d07b29008a8 1 parent d38417f
Jeremy Kemper authored January 17, 2007
2  activeresource/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Interpret 422 Unprocessable Entity as ResourceInvalid.  #7097 [dkubb]
  4
+
3 5
 * Mega documentation patches. #7025, #7069 [rwdaigle]
4 6
 
5 7
 * Base.exists?(id, options) and Base#exists? check whether the resource is found.  #6970 [rwdaigle]
12  activeresource/README
@@ -118,7 +118,7 @@ as the id of the ARes object.
118 118
   # when save is called on a new Person object.  An empty response is
119 119
   # is expected with a 'Location' header value:
120 120
   #
121  
-  # Response (200): Location: http://api.people.com:3000/people/2
  121
+  # Response (201): Location: http://api.people.com:3000/people/2
122 122
   #
123 123
   ryan = Person.new(:first => 'Ryan')
124 124
   ryan.new?  #=> true
@@ -184,7 +184,7 @@ exception.
184 184
 ==== Validation errors
185 185
 
186 186
 Creating and updating resources can lead to validation errors - i.e. 'First name cannot be empty' etc...
187  
-These types of errors are denoted in the response by a response code of 400 and the xml representation
  187
+These types of errors are denoted in the response by a response code of 422 and the xml representation
188 188
 of the validation errors.  The save operation will then fail (with a 'false' return value) and the
189 189
 validation errors can be accessed on the resource in question.
190 190
 
@@ -194,7 +194,7 @@ validation errors can be accessed on the resource in question.
194 194
   #
195 195
   # is requested with invalid values, the expected response is:
196 196
   #
197  
-  # Response (400):
  197
+  # Response (422):
198 198
   # <errors><error>First cannot be empty</error></errors>
199 199
   #
200 200
   ryan = Person.find(1)
@@ -210,13 +210,13 @@ If the underlying Http request for an ARes operation results in an error respons
210 210
 exception will be raised.  The following Http response codes will result in these exceptions:
211 211
 
212 212
    200 - 399: Valid response, no exception
213  
-   400: ActiveResource::ResourceInvalid (automatically caught by ARes validation)
214 213
    404: ActiveResource::ResourceNotFound
215 214
    409: ActiveResource::ResourceConflict
  215
+   422: ActiveResource::ResourceInvalid (rescued by save as validation errors)
216 216
    401 - 499: ActiveResource::ClientError
217 217
    500 - 599: ActiveResource::ServerError
218 218
 
219  
-   
  219
+
220 220
 === Authentication
221 221
 
222 222
 Many REST apis will require username/password authentication, usually in the form of
@@ -227,4 +227,4 @@ in the Url of the ARes site:
227 227
      self.site = "http://ryan:password@api.people.com:3000/"
228 228
    end
229 229
 
230  
-For obvious reasons it is best if such services are available over https.
  230
+For obvious reasons it is best if such services are available over https.
4  activeresource/lib/active_resource/connection.rb
@@ -90,10 +90,10 @@ def handle_response(response)
90 90
             response
91 91
           when 404
92 92
             raise(ResourceNotFound.new(response))
93  
-          when 400
94  
-            raise(ResourceInvalid.new(response))
95 93
           when 409
96 94
             raise(ResourceConflict.new(response))
  95
+          when 422
  96
+            raise(ResourceInvalid.new(response))
97 97
           when 401...500
98 98
             raise(ClientError.new(response))
99 99
           when 500...600
2  activeresource/test/base_errors_test.rb
@@ -4,7 +4,7 @@
4 4
 class BaseErrorsTest < Test::Unit::TestCase
5 5
   def setup
6 6
     ActiveResource::HttpMock.respond_to do |mock|
7  
-      mock.post "/people.xml", {}, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>", 400
  7
+      mock.post "/people.xml", {}, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>", 422
8 8
     end
9 9
     @person = Person.new(:name => '', :age => '')
10 10
     assert_equal @person.save, false
6  activeresource/test/connection_test.rb
@@ -36,12 +36,12 @@ def test_handle_response
36 36
     # 404 is a missing resource.
37 37
     assert_response_raises ActiveResource::ResourceNotFound, 404
38 38
 
39  
-    # 400 is a validation error
40  
-    assert_response_raises ActiveResource::ResourceInvalid, 400
41  
-
42 39
     # 409 is an optimistic locking error
43 40
     assert_response_raises ActiveResource::ResourceConflict, 409
44 41
 
  42
+    # 422 is a validation error
  43
+    assert_response_raises ActiveResource::ResourceInvalid, 422
  44
+
45 45
     # 4xx are client errors.
46 46
     [401, 499].each do |code|
47 47
       assert_response_raises ActiveResource::ClientError, code

0 notes on commit e00e6a2

Please sign in to comment.
Something went wrong with that request. Please try again.