Skip to content

Commit 52f23bb

Browse files
committed
Append socket error code constants for failed connections
1 parent bd4fe8a commit 52f23bb

29 files changed

+155
-86
lines changed

src/Connector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function connect($uri)
169169

170170
if (!isset($this->connectors[$scheme])) {
171171
return \React\Promise\reject(new \RuntimeException(
172-
'No connector available for URI scheme "' . $scheme . '"',
172+
'No connector available for URI scheme "' . $scheme . '" (EINVAL)',
173173
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
174174
));
175175
}

src/DnsConnector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function connect($uri)
3030

3131
if (!$parts || !isset($parts['host'])) {
3232
return Promise\reject(new \InvalidArgumentException(
33-
'Given URI "' . $original . '" is invalid',
33+
'Given URI "' . $original . '" is invalid (EINVAL)',
3434
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
3535
));
3636
}
@@ -95,7 +95,7 @@ function ($_, $reject) use (&$promise, &$resolved, $uri) {
9595
// reject DNS resolution with custom reason, otherwise rely on connection cancellation below
9696
if ($resolved === null) {
9797
$reject(new \RuntimeException(
98-
'Connection to ' . $uri . ' cancelled during DNS lookup',
98+
'Connection to ' . $uri . ' cancelled during DNS lookup (ECONNABORTED)',
9999
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
100100
));
101101
}

src/FdServer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function __construct($fd, LoopInterface $loop = null)
8282
}
8383
if (!\is_int($fd) || $fd < 0 || $fd >= \PHP_INT_MAX) {
8484
throw new \InvalidArgumentException(
85-
'Invalid FD number given',
85+
'Invalid FD number given (EINVAL)',
8686
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
8787
);
8888
}
@@ -99,7 +99,7 @@ public function __construct($fd, LoopInterface $loop = null)
9999
$errstr = isset($m[2]) ? $m[2] : $error['message'];
100100

101101
throw new \RuntimeException(
102-
'Failed to listen on FD ' . $fd . ': ' . $errstr,
102+
'Failed to listen on FD ' . $fd . ': ' . $errstr . SocketServer::errconst($errno),
103103
$errno
104104
);
105105
}
@@ -112,7 +112,7 @@ public function __construct($fd, LoopInterface $loop = null)
112112
$errstr = \function_exists('socket_strerror') ? \socket_strerror($errno) : 'Not a socket';
113113

114114
throw new \RuntimeException(
115-
'Failed to listen on FD ' . $fd . ': ' . $errstr,
115+
'Failed to listen on FD ' . $fd . ': ' . $errstr . ' (ENOTSOCK)',
116116
$errno
117117
);
118118
}
@@ -126,7 +126,7 @@ public function __construct($fd, LoopInterface $loop = null)
126126
$errstr = \function_exists('socket_strerror') ? \socket_strerror($errno) : 'Socket is connected';
127127

128128
throw new \RuntimeException(
129-
'Failed to listen on FD ' . $fd . ': ' . $errstr,
129+
'Failed to listen on FD ' . $fd . ': ' . $errstr . ' (EISCONN)',
130130
$errno
131131
);
132132
}

src/HappyEyeBallsConnectionBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function connect()
104104
})->then($lookupResolve(Message::TYPE_A));
105105
}, function ($_, $reject) use ($that, &$timer) {
106106
$reject(new \RuntimeException(
107-
'Connection to ' . $that->uri . ' cancelled' . (!$that->connectionPromises ? ' during DNS lookup' : ''),
107+
'Connection to ' . $that->uri . ' cancelled' . (!$that->connectionPromises ? ' during DNS lookup' : '') . ' (ECONNABORTED)',
108108
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
109109
));
110110
$_ = $reject = null;

src/HappyEyeBallsConnector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function connect($uri)
4242

4343
if (!$parts || !isset($parts['host'])) {
4444
return Promise\reject(new \InvalidArgumentException(
45-
'Given URI "' . $original . '" is invalid',
45+
'Given URI "' . $original . '" is invalid (EINVAL)',
4646
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
4747
));
4848
}

src/SecureConnector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function connect($uri)
3535
$parts = \parse_url($uri);
3636
if (!$parts || !isset($parts['scheme']) || $parts['scheme'] !== 'tls') {
3737
return Promise\reject(new \InvalidArgumentException(
38-
'Given URI "' . $uri . '" is invalid',
38+
'Given URI "' . $uri . '" is invalid (EINVAL)',
3939
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
4040
));
4141
}
@@ -109,7 +109,7 @@ function ($resolve, $reject) use ($promise) {
109109
function ($_, $reject) use (&$promise, $uri, &$connected) {
110110
if ($connected) {
111111
$reject(new \RuntimeException(
112-
'Connection to ' . $uri . ' cancelled during TLS handshake',
112+
'Connection to ' . $uri . ' cancelled during TLS handshake (ECONNABORTED)',
113113
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
114114
));
115115
}

src/SocketServer.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct($uri, array $context = array(), LoopInterface $loop
5353
} else {
5454
if (preg_match('#^(?:\w+://)?\d+$#', $uri)) {
5555
throw new \InvalidArgumentException(
56-
'Invalid URI given',
56+
'Invalid URI given (EINVAL)',
5757
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
5858
);
5959
}
@@ -121,6 +121,7 @@ public static function accept($socket)
121121
foreach (\get_defined_constants(false) as $name => $value) {
122122
if (\strpos($name, 'SOCKET_E') === 0 && \socket_strerror($value) === $errstr) {
123123
$errno = $value;
124+
$errstr .= ' (' . \substr($name, 7) . ')';
124125
break;
125126
}
126127
}
@@ -135,4 +136,37 @@ public static function accept($socket)
135136

136137
return $newSocket;
137138
}
139+
140+
/**
141+
* [Internal] Returns errno constant name for given errno value
142+
*
143+
* The errno value describes the type of error that has been encountered.
144+
* This method tries to look up the given errno value and find a matching
145+
* errno constant name which can be useful to provide more context and more
146+
* descriptive error messages. It goes through the list of known errno
147+
* constants when ext-sockets is available to find the matching errno
148+
* constant name.
149+
*
150+
* Because this method is used to append more context to error messages, the
151+
* constant name will be prefixed with a space and put between parenthesis
152+
* when found.
153+
*
154+
* @param int $errno
155+
* @return string e.g. ` (ECONNREFUSED)` or empty string if no matching const for the given errno could be found
156+
* @internal
157+
* @copyright Copyright (c) 2018 Christian Lück, taken from https://github.com/clue/errno with permission
158+
* @codeCoverageIgnore
159+
*/
160+
public static function errconst($errno)
161+
{
162+
if (\function_exists('socket_strerror')) {
163+
foreach (\get_defined_constants(false) as $name => $value) {
164+
if ($value === $errno && \strpos($name, 'SOCKET_E') === 0) {
165+
return ' (' . \substr($name, 7) . ')';
166+
}
167+
}
168+
}
169+
170+
return '';
171+
}
138172
}

src/StreamEncryption.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ public function toggleCrypto($socket, Deferred $deferred, $toggle, $method)
125125
if (\feof($socket) || $error === null) {
126126
// EOF or failed without error => connection closed during handshake
127127
$d->reject(new \UnexpectedValueException(
128-
'Connection lost during TLS handshake',
128+
'Connection lost during TLS handshake (ECONNRESET)',
129129
\defined('SOCKET_ECONNRESET') ? \SOCKET_ECONNRESET : 104
130130
));
131131
} else {
132132
// handshake failed with error message
133133
$d->reject(new \UnexpectedValueException(
134-
'Unable to complete TLS handshake: ' . $error
134+
$error
135135
));
136136
}
137137
} else {

src/TcpConnector.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public function connect($uri)
2828
$parts = \parse_url($uri);
2929
if (!$parts || !isset($parts['scheme'], $parts['host'], $parts['port']) || $parts['scheme'] !== 'tcp') {
3030
return Promise\reject(new \InvalidArgumentException(
31-
'Given URI "' . $uri . '" is invalid',
31+
'Given URI "' . $uri . '" is invalid (EINVAL)',
3232
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
3333
));
3434
}
3535

3636
$ip = \trim($parts['host'], '[]');
3737
if (false === \filter_var($ip, \FILTER_VALIDATE_IP)) {
3838
return Promise\reject(new \InvalidArgumentException(
39-
'Given URI "' . $uri . '" does not contain a valid host IP',
39+
'Given URI "' . $uri . '" does not contain a valid host IP (EINVAL)',
4040
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
4141
));
4242
}
@@ -91,7 +91,7 @@ public function connect($uri)
9191

9292
if (false === $stream) {
9393
return Promise\reject(new \RuntimeException(
94-
\sprintf("Connection to %s failed: %s", $uri, $errstr),
94+
'Connection to ' . $uri . ' failed: ' . $errstr . SocketServer::errconst($errno),
9595
$errno
9696
));
9797
}
@@ -132,7 +132,7 @@ public function connect($uri)
132132

133133
\fclose($stream);
134134
$reject(new \RuntimeException(
135-
'Connection to ' . $uri . ' failed: ' . $errstr,
135+
'Connection to ' . $uri . ' failed: ' . $errstr . SocketServer::errconst($errno),
136136
$errno
137137
));
138138
} else {
@@ -151,7 +151,7 @@ public function connect($uri)
151151
// @codeCoverageIgnoreEnd
152152

153153
throw new \RuntimeException(
154-
'Connection to ' . $uri . ' cancelled during TCP/IP handshake',
154+
'Connection to ' . $uri . ' cancelled during TCP/IP handshake (ECONNABORTED)',
155155
\defined('SOCKET_ECONNABORTED') ? \SOCKET_ECONNABORTED : 103
156156
);
157157
});

src/TcpServer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ public function __construct($uri, LoopInterface $loop = null, array $context = a
155155
// ensure URI contains TCP scheme, host and port
156156
if (!$parts || !isset($parts['scheme'], $parts['host'], $parts['port']) || $parts['scheme'] !== 'tcp') {
157157
throw new \InvalidArgumentException(
158-
'Invalid URI "' . $uri . '" given',
158+
'Invalid URI "' . $uri . '" given (EINVAL)',
159159
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
160160
);
161161
}
162162

163163
if (false === \filter_var(\trim($parts['host'], '[]'), \FILTER_VALIDATE_IP)) {
164164
throw new \InvalidArgumentException(
165-
'Given URI "' . $uri . '" does not contain a valid host IP',
165+
'Given URI "' . $uri . '" does not contain a valid host IP (EINVAL)',
166166
\defined('SOCKET_EINVAL') ? \SOCKET_EINVAL : 22
167167
);
168168
}
@@ -176,7 +176,7 @@ public function __construct($uri, LoopInterface $loop = null, array $context = a
176176
);
177177
if (false === $this->master) {
178178
throw new \RuntimeException(
179-
'Failed to listen on "' . $uri . '": ' . $errstr,
179+
'Failed to listen on "' . $uri . '": ' . $errstr . SocketServer::errconst($errno),
180180
$errno
181181
);
182182
}

0 commit comments

Comments
 (0)