From 1ceccdeb7f86898a9e511e934ea6b0863d30590d Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 14 Dec 2007 23:09:46 +0000 Subject: Added more specific exceptions for 400, 401, and 403 (all descending from ClientError so existing rescues will work) (closes #10326) [trek] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8390 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activeresource/CHANGELOG | 2 ++ activeresource/lib/active_resource/connection.rb | 15 +++++++++++++++ activeresource/test/connection_test.rb | 11 ++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) (limited to 'activeresource') diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index b02b6161e3..d3d20a68db 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added more specific exceptions for 400, 401, and 403 (all descending from ClientError so existing rescues will work) #10326 [trek] + * Correct empty response handling. #10445 [seangeo] diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index fba393ff89..2bf83b1615 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -26,6 +26,15 @@ module ActiveResource # 4xx Client Error class ClientError < ConnectionError; end # :nodoc: + # 400 Bad Request + class BadRequest < ClientError; end # :nodoc + + # 401 Unauthorized + class UnauthorizedAccess < ClientError; end # :nodoc + + # 403 Forbidden + class ForbiddenAccess < ClientError; end # :nodoc + # 404 Not Found class ResourceNotFound < ClientError; end # :nodoc: @@ -110,6 +119,12 @@ module ActiveResource raise(Redirection.new(response)) when 200...400 response + when 400 + raise(BadRequest.new(response)) + when 401 + raise(UnauthorizedAccess.new(response)) + when 403 + raise(ForbiddenAccess.new(response)) when 404 raise(ResourceNotFound.new(response)) when 405 diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb index 09e80f95c1..ffad974401 100644 --- a/activeresource/test/connection_test.rb +++ b/activeresource/test/connection_test.rb @@ -38,6 +38,15 @@ class ConnectionTest < Test::Unit::TestCase assert_equal expected, handle_response(expected) end + # 400 is a bad request (e.g. malformed URI or missing request parameter) + assert_response_raises ActiveResource::BadRequest, 400 + + # 401 is an unauthorized request + assert_response_raises ActiveResource::UnauthorizedAccess, 401 + + # 403 is a forbidden requst (and authorizing will not help) + assert_response_raises ActiveResource::ForbiddenAccess, 403 + # 404 is a missing resource. assert_response_raises ActiveResource::ResourceNotFound, 404 @@ -51,7 +60,7 @@ class ConnectionTest < Test::Unit::TestCase assert_response_raises ActiveResource::ResourceInvalid, 422 # 4xx are client errors. - [401, 499].each do |code| + [402, 499].each do |code| assert_response_raises ActiveResource::ClientError, code end -- cgit v1.2.3