diff options
-rw-r--r-- | activeresource/lib/active_resource/connection.rb | 9 | ||||
-rw-r--r-- | activeresource/test/connection_test.rb | 6 |
2 files changed, 11 insertions, 4 deletions
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index 5aef6f4d42..07f3c98eda 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -14,10 +14,15 @@ module ActiveResource end def to_s - "Failed with #{response.code}" + "Failed with #{response.code} #{response.message if response.respond_to?(:message)}" end end + # 3xx Redirection + class Redirection < ConnectionError # :nodoc: + def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end + end + # 4xx Client Error class ClientError < ConnectionError; end # :nodoc: @@ -107,6 +112,8 @@ module ActiveResource # Handles response and error codes from remote service. def handle_response(response) case response.code.to_i + when 301,302 + raise(Redirection.new(response)) when 200...400 response when 404 diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb index 24893f8cd3..68d57018bf 100644 --- a/activeresource/test/connection_test.rb +++ b/activeresource/test/connection_test.rb @@ -66,12 +66,12 @@ class ConnectionTest < Test::Unit::TestCase end end - ResponseHeaderStub = Struct.new(:code, 'Allow') + ResponseHeaderStub = Struct.new(:code, :message, 'Allow') def test_should_return_allowed_methods_for_method_no_allowed_exception begin - handle_response ResponseHeaderStub.new(405, "GET, POST") + handle_response ResponseHeaderStub.new(405, "HTTP Failed...", "GET, POST") rescue ActiveResource::MethodNotAllowed => e - assert_equal "Failed with 405", e.message + assert_equal "Failed with 405 HTTP Failed...", e.message assert_equal [:get, :post], e.allowed_methods end end |