From f2d7d408a2f1675b493cfc3cbabc2e605f2a6339 Mon Sep 17 00:00:00 2001 From: Jens Balvig Date: Tue, 12 Dec 2023 10:33:48 +0900 Subject: [PATCH 1/2] Raise ActiveResource::ConnectionError on Errno::ECONNREFUSED --- lib/active_resource/connection.rb | 2 ++ lib/active_resource/exceptions.rb | 8 ++++++++ test/cases/connection_test.rb | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/lib/active_resource/connection.rb b/lib/active_resource/connection.rb index e997a0a2f6..ecdb4f3255 100644 --- a/lib/active_resource/connection.rb +++ b/lib/active_resource/connection.rb @@ -127,6 +127,8 @@ def request(method, path, *arguments) raise TimeoutError.new(e.message) rescue OpenSSL::SSL::SSLError => e raise SSLError.new(e.message) + rescue Errno::ECONNREFUSED => e + raise ConnectionRefusedError.new(e.message) end # Handles response and error codes from the remote service. diff --git a/lib/active_resource/exceptions.rb b/lib/active_resource/exceptions.rb index 08daecafc3..5c71f32591 100644 --- a/lib/active_resource/exceptions.rb +++ b/lib/active_resource/exceptions.rb @@ -35,6 +35,14 @@ def initialize(message) def to_s; @message ; end end + # Raised when a Errno::ECONNREFUSED occurs. + class ConnectionRefusedError < ConnectionError + def initialize(message) + @message = message + end + def to_s; @message ; end + end + # 3xx Redirection class Redirection < ConnectionError # :nodoc: def to_s diff --git a/test/cases/connection_test.rb b/test/cases/connection_test.rb index c879f4cd79..b7ee8dc880 100644 --- a/test/cases/connection_test.rb +++ b/test/cases/connection_test.rb @@ -282,6 +282,13 @@ def test_ssl_error assert_raise(ActiveResource::SSLError) { @conn.get("/people/1.json") } end + def test_handle_econnrefused + http = Net::HTTP.new("") + @conn.expects(:http).returns(http) + http.expects(:get).raises(Errno::ECONNREFUSED, "Failed to open TCP connection") + assert_raise(ActiveResource::ConnectionRefusedError) { @conn.get("/people/1.json") } + end + def test_auth_type_can_be_string @conn.auth_type = "digest" assert_equal(:digest, @conn.auth_type) From e8b9e1cb93d721aa65274d85ceebd0468497621b Mon Sep 17 00:00:00 2001 From: Jens Balvig Date: Mon, 29 Jan 2024 14:06:09 +0900 Subject: [PATCH 2/2] Preserve ability to inherit from original error https://github.com/rails/activeresource/pull/396#issuecomment-1908843468 --- lib/active_resource/exceptions.rb | 7 +------ test/cases/connection_test.rb | 7 +++++++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/active_resource/exceptions.rb b/lib/active_resource/exceptions.rb index 5c71f32591..a53c783e86 100644 --- a/lib/active_resource/exceptions.rb +++ b/lib/active_resource/exceptions.rb @@ -36,12 +36,7 @@ def to_s; @message ; end end # Raised when a Errno::ECONNREFUSED occurs. - class ConnectionRefusedError < ConnectionError - def initialize(message) - @message = message - end - def to_s; @message ; end - end + class ConnectionRefusedError < Errno::ECONNREFUSED; end # 3xx Redirection class Redirection < ConnectionError # :nodoc: diff --git a/test/cases/connection_test.rb b/test/cases/connection_test.rb index b7ee8dc880..452ca9bdb5 100644 --- a/test/cases/connection_test.rb +++ b/test/cases/connection_test.rb @@ -289,6 +289,13 @@ def test_handle_econnrefused assert_raise(ActiveResource::ConnectionRefusedError) { @conn.get("/people/1.json") } end + def test_handle_econnrefused_with_backwards_compatible_error + http = Net::HTTP.new("") + @conn.expects(:http).returns(http) + http.expects(:get).raises(Errno::ECONNREFUSED, "Failed to open TCP connection") + assert_raise(Errno::ECONNREFUSED) { @conn.get("/people/1.json") } + end + def test_auth_type_can_be_string @conn.auth_type = "digest" assert_equal(:digest, @conn.auth_type)