mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Interpret 422 Unprocessable Entity as ResourceInvalid. Closes #7097.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5967 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
d38417fc02
commit
e00e6a2941
5 changed files with 14 additions and 12 deletions
|
@ -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]
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -90,10 +90,10 @@ module ActiveResource
|
|||
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
|
||||
|
|
|
@ -4,7 +4,7 @@ require "fixtures/person"
|
|||
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
|
||||
|
|
|
@ -36,12 +36,12 @@ class ConnectionTest < Test::Unit::TestCase
|
|||
# 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
|
||||
|
|
Loading…
Reference in a new issue