From cee4e2a93bd00712ea22e8d9f4f70f8106b2a6cb Mon Sep 17 00:00:00 2001 From: Max Perry Date: Tue, 4 Feb 2025 10:04:03 +0000 Subject: [PATCH] Catch and raise EHOSTUNREACH and EADDRNOTAVAI syscall errors as ActiveUtils::ConnectionError --- CHANGELOG.md | 3 +++ lib/active_utils/network_connection_retries.rb | 4 +++- lib/active_utils/version.rb | 2 +- test/unit/network_connection_retries_test.rb | 18 ++++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92e8a13..7e3842f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # ActiveUtils changelog +### Version 3.5.0 (February 04, 2025) +- Catch and raise `EHOSTUNREACH` and `EADDRNOTAVAIL` syscall errors as `ActiveUtils::ConnectionError` + ### Version 3.4.2 (January 15, 2025) - Remove numeric as a required param for `ActiveUtils::Country` diff --git a/lib/active_utils/network_connection_retries.rb b/lib/active_utils/network_connection_retries.rb index f2d3541..2bc4633 100644 --- a/lib/active_utils/network_connection_retries.rb +++ b/lib/active_utils/network_connection_retries.rb @@ -7,7 +7,9 @@ module NetworkConnectionRetries Timeout::Error => "The connection to the remote server timed out", Errno::ETIMEDOUT => "The connection to the remote server timed out", SocketError => "The connection to the remote server could not be established", - OpenSSL::SSL::SSLError => "The SSL connection to the remote server could not be established" + OpenSSL::SSL::SSLError => "The SSL connection to the remote server could not be established", + Errno::EHOSTUNREACH => "The remote server could not be reached", + Errno::EADDRNOTAVAIL => "The remote server address is not available" } DEFAULT_RETRY_ERRORS = { diff --git a/lib/active_utils/version.rb b/lib/active_utils/version.rb index a0e97ed..4ecab75 100644 --- a/lib/active_utils/version.rb +++ b/lib/active_utils/version.rb @@ -1,3 +1,3 @@ module ActiveUtils - VERSION = "3.4.2" + VERSION = "3.5.0" end diff --git a/test/unit/network_connection_retries_test.rb b/test/unit/network_connection_retries_test.rb index 80ffed5..9d09bb3 100644 --- a/test/unit/network_connection_retries_test.rb +++ b/test/unit/network_connection_retries_test.rb @@ -32,6 +32,24 @@ def test_econnreset_raises_correctly assert_equal "The remote server reset the connection", raised.message end + def test_ehostunreach_raises_correctly + raised = assert_raises(ActiveUtils::ConnectionError) do + retry_exceptions do + raise Errno::EHOSTUNREACH + end + end + assert_equal "The remote server could not be reached", raised.message + end + + def test_eaddrnotavail_raises_correctly + raised = assert_raises(ActiveUtils::ConnectionError) do + retry_exceptions do + raise Errno::EADDRNOTAVAIL + end + end + assert_equal "The remote server address is not available", raised.message + end + def test_timeout_errors_raise_correctly exceptions = [Timeout::Error, Errno::ETIMEDOUT] if RUBY_VERSION >= '2.0.0'