Skip to content

Commit c4086b5

Browse files
committed
Include underlying connection error when transport connection fails
1 parent 2c91902 commit c4086b5

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Query/TcpTransportExecutor.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,19 @@ public function handleWritable()
227227
if ($this->readPending === false) {
228228
$name = @\stream_socket_get_name($this->socket, true);
229229
if ($name === false) {
230-
$this->closeError('Connection to DNS server ' . $this->nameserver . ' rejected');
230+
// Connection failed? Check socket error if available for underlying errno/errstr.
231+
// @codeCoverageIgnoreStart
232+
if (\function_exists('socket_import_stream')) {
233+
$socket = \socket_import_stream($this->socket);
234+
$errno = \socket_get_option($socket, \SOL_SOCKET, \SO_ERROR);
235+
$errstr = \socket_strerror($errno);
236+
} else {
237+
$errno = \defined('SOCKET_ECONNREFUSED') ? \SOCKET_ECONNREFUSED : 111;
238+
$errstr = 'Connection refused';
239+
}
240+
// @codeCoverageIgnoreEnd
241+
242+
$this->closeError('Unable to connect to DNS server ' . $this->nameserver . ' (' . $errstr . ')', $errno);
231243
return;
232244
}
233245

tests/Query/TcpTransportExecutorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ function ($e) use (&$exception) {
265265

266266
/** @var \RuntimeException $exception */
267267
$this->assertInstanceOf('RuntimeException', $exception);
268-
$this->assertEquals('DNS query for google.com (A) failed: Connection to DNS server tcp://127.0.0.1:1 rejected', $exception->getMessage());
268+
$this->assertEquals('DNS query for google.com (A) failed: Unable to connect to DNS server tcp://127.0.0.1:1 (Connection refused)', $exception->getMessage());
269+
$this->assertEquals(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $exception->getCode());
269270
}
270271

271272
public function testQueryStaysPendingWhenClientCanNotSendExcessiveMessageInOneChunk()

0 commit comments

Comments
 (0)