diff --git a/ext/socket/init.c b/ext/socket/init.c index 5ca0cdbb1d044c..10459710c249b0 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 12223bcc06fa8b..09c8db6049fa9d 100644 --- a/test/fiber/test_address_resolve.rb +++ b/test/fiber/test_address_resolve.rb @@ -179,7 +179,7 @@ def test_addrinfo_getaddrinfo_non_existing_domain_blocking 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 832a1f8d05bcb9..c61764d76d63b8 100644 --- a/test/socket/test_addrinfo.rb +++ b/test/socket/test_addrinfo.rb @@ -103,7 +103,7 @@ def test_addrinfo_predicates 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 @@ def test_family_addrinfo 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 56457235580c54..01f790ea35578c 100644 --- a/test/socket/test_socket.rb +++ b/test/socket/test_socket.rb @@ -91,20 +91,20 @@ def test_bind 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 @@ def test_udp_recvmsg_truncation 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)