Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 jeremy authored
2  activeresource/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* Interpret 422 Unprocessable Entity as ResourceInvalid. #7097 [dkubb]
+
* Mega documentation patches. #7025, #7069 [rwdaigle]
* Base.exists?(id, options) and Base#exists? check whether the resource is found. #6970 [rwdaigle]
12 activeresource/README
View
@@ -118,7 +118,7 @@ as the id of the ARes object.
# when save is called on a new Person object. An empty response is
# is expected with a 'Location' header value:
#
- # Response (200): Location: http://api.people.com:3000/people/2
+ # Response (201): Location: http://api.people.com:3000/people/2
#
ryan = Person.new(:first => 'Ryan')
ryan.new? #=> true
@@ -184,7 +184,7 @@ exception.
==== Validation errors
Creating and updating resources can lead to validation errors - i.e. 'First name cannot be empty' etc...
-These types of errors are denoted in the response by a response code of 400 and the xml representation
+These types of errors are denoted in the response by a response code of 422 and the xml representation
of the validation errors. The save operation will then fail (with a 'false' return value) and the
validation errors can be accessed on the resource in question.
@@ -194,7 +194,7 @@ validation errors can be accessed on the resource in question.
#
# is requested with invalid values, the expected response is:
#
- # Response (400):
+ # Response (422):
# <errors><error>First cannot be empty</error></errors>
#
ryan = Person.find(1)
@@ -210,13 +210,13 @@ If the underlying Http request for an ARes operation results in an error respons
exception will be raised. The following Http response codes will result in these exceptions:
200 - 399: Valid response, no exception
- 400: ActiveResource::ResourceInvalid (automatically caught by ARes validation)
404: ActiveResource::ResourceNotFound
409: ActiveResource::ResourceConflict
+ 422: ActiveResource::ResourceInvalid (rescued by save as validation errors)
401 - 499: ActiveResource::ClientError
500 - 599: ActiveResource::ServerError
-
+
=== Authentication
Many REST apis will require username/password authentication, usually in the form of
@@ -227,4 +227,4 @@ in the Url of the ARes site:
self.site = "http://ryan:password@api.people.com:3000/"
end
-For obvious reasons it is best if such services are available over https.
+For obvious reasons it is best if such services are available over https.
4 activeresource/lib/active_resource/connection.rb
View
@@ -90,10 +90,10 @@ def handle_response(response)
response
when 404
raise(ResourceNotFound.new(response))
- when 400
- raise(ResourceInvalid.new(response))
when 409
raise(ResourceConflict.new(response))
+ when 422
+ raise(ResourceInvalid.new(response))
when 401...500
raise(ClientError.new(response))
when 500...600
2  activeresource/test/base_errors_test.rb
View
@@ -4,7 +4,7 @@
class BaseErrorsTest < Test::Unit::TestCase
def setup
ActiveResource::HttpMock.respond_to do |mock|
- 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
+ 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
end
@person = Person.new(:name => '', :age => '')
assert_equal @person.save, false
6 activeresource/test/connection_test.rb
View
@@ -36,12 +36,12 @@ def test_handle_response
# 404 is a missing resource.
assert_response_raises ActiveResource::ResourceNotFound, 404
- # 400 is a validation error
- assert_response_raises ActiveResource::ResourceInvalid, 400
-
# 409 is an optimistic locking error
assert_response_raises ActiveResource::ResourceConflict, 409
+ # 422 is a validation error
+ assert_response_raises ActiveResource::ResourceInvalid, 422
+
# 4xx are client errors.
[401, 499].each do |code|
assert_response_raises ActiveResource::ClientError, code
Please sign in to comment.
Something went wrong with that request. Please try again.