aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-12-14 23:09:46 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-12-14 23:09:46 +0000
commit1ceccdeb7f86898a9e511e934ea6b0863d30590d (patch)
tree51df80c22b0ea40c7893e259884be2204339669e /activeresource
parentf5c17790e1a16d92d29f60a5b308b3895e5b94c5 (diff)
downloadrails-1ceccdeb7f86898a9e511e934ea6b0863d30590d.tar.gz
rails-1ceccdeb7f86898a9e511e934ea6b0863d30590d.tar.bz2
rails-1ceccdeb7f86898a9e511e934ea6b0863d30590d.zip
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
Diffstat (limited to 'activeresource')
-rw-r--r--activeresource/CHANGELOG2
-rw-r--r--activeresource/lib/active_resource/connection.rb15
-rw-r--r--activeresource/test/connection_test.rb11
3 files changed, 27 insertions, 1 deletions
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