Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise ActiveResource::ConnectionRefusedError on Errno::ECONNREFUSED #396

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/active_resource/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lib/active_resource/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def initialize(message)
def to_s; @message ; end
end

# Raised when a Errno::ECONNREFUSED occurs.
class ConnectionRefusedError < Errno::ECONNREFUSED; end

# 3xx Redirection
class Redirection < ConnectionError # :nodoc:
def to_s
Expand Down
14 changes: 14 additions & 0 deletions test/cases/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,20 @@ 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_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)
Expand Down
Loading