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
This commit is contained in:
David Heinemeier Hansson 2007-12-14 23:09:46 +00:00
parent f5c17790e1
commit 1ceccdeb7f
3 changed files with 27 additions and 1 deletions

@ -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]

@ -26,6 +26,15 @@ def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super;
# 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 @@ def handle_response(response)
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

@ -38,6 +38,15 @@ def test_handle_response
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 @@ def test_handle_response
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