From e00e6a29410e4ae75abc0480a4d79d07b29008a8 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 17 Jan 2007 00:46:32 +0000 Subject: Interpret 422 Unprocessable Entity as ResourceInvalid. Closes #7097. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5967 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activeresource/CHANGELOG | 2 ++ activeresource/README | 12 ++++++------ activeresource/lib/active_resource/connection.rb | 4 ++-- activeresource/test/base_errors_test.rb | 2 +- activeresource/test/connection_test.rb | 6 +++--- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index 1f30c14f6f..70bc5675e2 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -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] diff --git a/activeresource/README b/activeresource/README index a657e3a471..702ab7efed 100644 --- a/activeresource/README +++ b/activeresource/README @@ -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): # First cannot be empty # 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. \ No newline at end of file +For obvious reasons it is best if such services are available over https. diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index 528a7cc678..8e25ec5eb6 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -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 diff --git a/activeresource/test/base_errors_test.rb b/activeresource/test/base_errors_test.rb index c6b840ebe1..3527eb2353 100644 --- a/activeresource/test/base_errors_test.rb +++ b/activeresource/test/base_errors_test.rb @@ -4,7 +4,7 @@ require "fixtures/person" class BaseErrorsTest < Test::Unit::TestCase def setup ActiveResource::HttpMock.respond_to do |mock| - mock.post "/people.xml", {}, "Age can't be blankName can't be blankName must start with a letterPerson quota full for today.", 400 + mock.post "/people.xml", {}, "Age can't be blankName can't be blankName must start with a letterPerson quota full for today.", 422 end @person = Person.new(:name => '', :age => '') assert_equal @person.save, false diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb index 99f0b8de89..8fcbf089de 100644 --- a/activeresource/test/connection_test.rb +++ b/activeresource/test/connection_test.rb @@ -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 -- cgit v1.2.3