aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMisaki Shioi <shioi.mm@gmail.com>2023-11-23 16:49:41 +0900
committerYusuke Endoh <mame@ruby-lang.org>2023-11-30 13:27:19 +0900
commit52f6de41961d511e82826d6266441d91a92dcd59 (patch)
tree07b25e7fbfa7a013615b22e6704d137619a0c1af
parente9050270d7d8b1bc6be2c1352772b906031adc6e (diff)
downloadruby-52f6de41961d511e82826d6266441d91a92dcd59.tar.gz
Replace SocketError with Socket::ResolutionError in rsock_raise_socket_error
rsock_raise_socket_error is called only when getaddrinfo and getaddrname fail
-rw-r--r--ext/socket/init.c8
-rw-r--r--test/fiber/test_address_resolve.rb2
-rw-r--r--test/socket/test_addrinfo.rb4
-rw-r--r--test/socket/test_socket.rb14
4 files changed, 20 insertions, 8 deletions
diff --git a/ext/socket/init.c b/ext/socket/init.c
index 5ca0cdbb1d..10459710c2 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -50,10 +50,14 @@ rsock_raise_socket_error(const char *reason, int error)
VALUE msg = rb_sprintf("%s: ", reason);
if (!enc) enc = rb_default_internal_encoding();
rb_str_concat(msg, rb_w32_conv_from_wchar(gai_strerrorW(error), enc));
- rb_exc_raise(rb_exc_new_str(rb_eSocket, msg));
#else
- rb_raise(rb_eSocket, "%s: %s", reason, gai_strerror(error));
+ VALUE msg = rb_sprintf("%s: %s", reason, gai_strerror(error));
#endif
+
+ StringValue(msg);
+ VALUE self = rb_class_new_instance(1, &msg, rb_eResolution);
+ rb_ivar_set(self, id_error_code, INT2NUM(error));
+ rb_exc_raise(self);
}
#if defined __APPLE__
diff --git a/test/fiber/test_address_resolve.rb b/test/fiber/test_address_resolve.rb
index 12223bcc06..09c8db6049 100644
--- a/test/fiber/test_address_resolve.rb
+++ b/test/fiber/test_address_resolve.rb
@@ -179,7 +179,7 @@ class TestAddressResolve < Test::Unit::TestCase
Fiber.set_scheduler scheduler
Fiber.schedule do
- assert_raise(SocketError) {
+ assert_raise(Socket::ResolutionError) {
Addrinfo.getaddrinfo("non-existing-domain.abc", nil)
}
end
diff --git a/test/socket/test_addrinfo.rb b/test/socket/test_addrinfo.rb
index 832a1f8d05..c61764d76d 100644
--- a/test/socket/test_addrinfo.rb
+++ b/test/socket/test_addrinfo.rb
@@ -103,7 +103,7 @@ class TestSocketAddrinfo < Test::Unit::TestCase
end
def test_error_message
- e = assert_raise_with_message(SocketError, /getaddrinfo/) do
+ e = assert_raise_with_message(Socket::ResolutionError, /getaddrinfo/) do
Addrinfo.ip("...")
end
m = e.message
@@ -357,7 +357,7 @@ class TestSocketAddrinfo < Test::Unit::TestCase
ai = Addrinfo.unix("/testdir/sock").family_addrinfo("/testdir/sock2")
assert_equal("/testdir/sock2", ai.unix_path)
assert_equal(Socket::SOCK_STREAM, ai.socktype)
- assert_raise(SocketError) { Addrinfo.tcp("0.0.0.0", 4649).family_addrinfo("::1", 80) }
+ assert_raise(Socket::ResolutionError) { Addrinfo.tcp("0.0.0.0", 4649).family_addrinfo("::1", 80) }
end
def random_port
diff --git a/test/socket/test_socket.rb b/test/socket/test_socket.rb
index 5645723558..01f790ea35 100644
--- a/test/socket/test_socket.rb
+++ b/test/socket/test_socket.rb
@@ -91,20 +91,20 @@ class TestSocket < Test::Unit::TestCase
def test_getaddrinfo
# This should not send a DNS query because AF_UNIX.
- assert_raise(SocketError) { Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX") }
+ assert_raise(Socket::ResolutionError) { Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX") }
end
def test_getaddrinfo_raises_no_errors_on_port_argument_of_0 # [ruby-core:29427]
assert_nothing_raised('[ruby-core:29427]'){ Socket.getaddrinfo('localhost', 0, Socket::AF_INET, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) }
assert_nothing_raised('[ruby-core:29427]'){ Socket.getaddrinfo('localhost', '0', Socket::AF_INET, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) }
assert_nothing_raised('[ruby-core:29427]'){ Socket.getaddrinfo('localhost', '00', Socket::AF_INET, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) }
- assert_raise(SocketError, '[ruby-core:29427]'){ Socket.getaddrinfo(nil, nil, Socket::AF_INET, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) }
+ assert_raise(Socket::ResolutionError, '[ruby-core:29427]'){ Socket.getaddrinfo(nil, nil, Socket::AF_INET, Socket::SOCK_STREAM, nil, Socket::AI_CANONNAME) }
assert_nothing_raised('[ruby-core:29427]'){ TCPServer.open('localhost', 0) {} }
end
def test_getnameinfo
- assert_raise(SocketError) { Socket.getnameinfo(["AF_UNIX", 80, "0.0.0.0"]) }
+ assert_raise(Socket::ResolutionError) { Socket.getnameinfo(["AF_UNIX", 80, "0.0.0.0"]) }
assert_raise(ArgumentError) {Socket.getnameinfo(["AF_INET", "http\0", "example.net"])}
assert_raise(ArgumentError) {Socket.getnameinfo(["AF_INET", "http", "example.net\0"])}
end
@@ -770,4 +770,12 @@ class TestSocket < Test::Unit::TestCase
s2.close
end
+ def test_resolurion_error_error_code
+ begin
+ Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX")
+ rescue => e
+ assert_equal(e.error_code, Socket::EAI_FAMILY)
+ end
+ end
+
end if defined?(Socket)