Skip to content

Improve error reporting, include Redis URI and socket error codes in all connection errors #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use socket error codes (errnos) for connection rejections
  • Loading branch information
clue committed Aug 29, 2021
commit e57ccc234f9be5db1d5048864a82238651fb1036
23 changes: 15 additions & 8 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public function createClient($uri)

$uri = preg_replace(array('/(:)[^:\/]*(@)/', '/([?&]password=).*?($|&)/'), '$1***$2', $uri);
if ($parts === false || !isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('redis', 'rediss', 'redis+unix'))) {
return \React\Promise\reject(new \InvalidArgumentException('Invalid Redis URI given'));
return \React\Promise\reject(new \InvalidArgumentException(
'Invalid Redis URI given (EINVAL)',
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22
));
}

$args = array();
Expand All @@ -73,7 +76,10 @@ public function createClient($uri)

$deferred = new Deferred(function ($_, $reject) use ($connecting, $uri) {
// connection cancelled, start with rejecting attempt, then clean up
$reject(new \RuntimeException('Connection to ' . $uri . ' cancelled'));
$reject(new \RuntimeException(
'Connection to ' . $uri . ' cancelled (ECONNABORTED)',
defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103
));

// either close successful connection or cancel pending connection attempt
$connecting->then(function (ConnectionInterface $connection) {
Expand All @@ -88,7 +94,7 @@ public function createClient($uri)
}, function (\Exception $e) use ($uri) {
throw new \RuntimeException(
'Connection to ' . $uri . ' failed: ' . $e->getMessage(),
0,
$e->getCode(),
$e
);
});
Expand All @@ -106,8 +112,8 @@ function (\Exception $e) use ($client, $uri) {
$client->close();

throw new \RuntimeException(
'Connection to ' . $uri . ' failed during AUTH command: ' . $e->getMessage(),
0,
'Connection to ' . $uri . ' failed during AUTH command: ' . $e->getMessage() . ' (EACCES)',
defined('SOCKET_EACCES') ? SOCKET_EACCES : 13,
$e
);
}
Expand All @@ -127,8 +133,8 @@ function (\Exception $e) use ($client, $uri) {
$client->close();

throw new \RuntimeException(
'Connection to ' . $uri . ' failed during SELECT command: ' . $e->getMessage(),
0,
'Connection to ' . $uri . ' failed during SELECT command: ' . $e->getMessage() . ' (ENOENT)',
defined('SOCKET_ENOENT') ? SOCKET_ENOENT : 2,
$e
);
}
Expand All @@ -147,7 +153,8 @@ function (\Exception $e) use ($client, $uri) {
return \React\Promise\Timer\timeout($deferred->promise(), $timeout, $this->loop)->then(null, function ($e) use ($uri) {
if ($e instanceof TimeoutException) {
throw new \RuntimeException(
'Connection to ' . $uri . ' timed out after ' . $e->getTimeout() . ' seconds'
'Connection to ' . $uri . ' timed out after ' . $e->getTimeout() . ' seconds (ETIMEDOUT)',
defined('SOCKET_ETIMEDOUT') ? SOCKET_ETIMEDOUT : 110
);
}
throw $e;
Expand Down
5 changes: 4 additions & 1 deletion src/LazyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ private function client()
public function __call($name, $args)
{
if ($this->closed) {
return \React\Promise\reject(new \RuntimeException('Connection closed'));
return \React\Promise\reject(new \RuntimeException(
'Connection closed (ENOTCONN)',
defined('SOCKET_ENOTCONN') ? SOCKET_ENOTCONN : 107
));
}

$that = $this;
Expand Down
52 changes: 33 additions & 19 deletions src/StreamingClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

namespace Clue\React\Redis;

use Evenement\EventEmitter;
use Clue\Redis\Protocol\Parser\ParserInterface;
use Clue\Redis\Protocol\Parser\ParserException;
use Clue\Redis\Protocol\Serializer\SerializerInterface;
use Clue\Redis\Protocol\Factory as ProtocolFactory;
use UnderflowException;
use RuntimeException;
use InvalidArgumentException;
use React\Promise\Deferred;
use Clue\Redis\Protocol\Model\ErrorReply;
use Clue\Redis\Protocol\Model\ModelInterface;
use Clue\Redis\Protocol\Model\MultiBulkReply;
use Clue\Redis\Protocol\Parser\ParserException;
use Clue\Redis\Protocol\Parser\ParserInterface;
use Clue\Redis\Protocol\Serializer\SerializerInterface;
use Evenement\EventEmitter;
use React\Promise\Deferred;
use React\Stream\DuplexStreamInterface;

/**
Expand Down Expand Up @@ -47,18 +44,20 @@ public function __construct(DuplexStreamInterface $stream, ParserInterface $pars
$stream->on('data', function($chunk) use ($parser, $that) {
try {
$models = $parser->pushIncoming($chunk);
}
catch (ParserException $error) {
$that->emit('error', array($error));
} catch (ParserException $error) {
$that->emit('error', array(new \UnexpectedValueException(
'Invalid data received: ' . $error->getMessage() . ' (EBADMSG)',
defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG : 77,
$error
)));
$that->close();
return;
}

foreach ($models as $data) {
try {
$that->handleMessage($data);
}
catch (UnderflowException $error) {
} catch (\UnderflowException $error) {
$that->emit('error', array($error));
$that->close();
return;
Expand All @@ -84,11 +83,20 @@ public function __call($name, $args)
static $pubsubs = array('subscribe', 'unsubscribe', 'psubscribe', 'punsubscribe');

if ($this->ending) {
$request->reject(new RuntimeException('Connection closed'));
$request->reject(new \RuntimeException(
'Connection ' . ($this->closed ? 'closed' : 'closing'). ' (ENOTCONN)',
defined('SOCKET_ENOTCONN') ? SOCKET_ENOTCONN : 107
));
} elseif (count($args) !== 1 && in_array($name, $pubsubs)) {
$request->reject(new InvalidArgumentException('PubSub commands limited to single argument'));
$request->reject(new \InvalidArgumentException(
'PubSub commands limited to single argument (EINVAL)',
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22
));
} elseif ($name === 'monitor') {
$request->reject(new \BadMethodCallException('MONITOR command explicitly not supported'));
$request->reject(new \BadMethodCallException(
'MONITOR command explicitly not supported (ENOTSUP)',
defined('SOCKET_ENOTSUP') ? SOCKET_ENOTSUP : (defined('SOCKET_EOPNOTSUPP') ? SOCKET_EOPNOTSUPP : 95)
));
} else {
$this->stream->write($this->serializer->getRequestMessage($name, $args));
$this->requests []= $request;
Expand Down Expand Up @@ -131,7 +139,10 @@ public function handleMessage(ModelInterface $message)
}

if (!$this->requests) {
throw new UnderflowException('Unexpected reply received, no matching request found');
throw new \UnderflowException(
'Unexpected reply received, no matching request found (ENOMSG)',
defined('SOCKET_ENOMSG') ? SOCKET_ENOMSG : 42
);
}

$request = array_shift($this->requests);
Expand Down Expand Up @@ -173,8 +184,11 @@ public function close()
// reject all remaining requests in the queue
while($this->requests) {
$request = array_shift($this->requests);
/* @var $request Request */
$request->reject(new RuntimeException('Connection closing'));
/* @var $request Deferred */
$request->reject(new \RuntimeException(
'Connection closing (ENOTCONN)',
defined('SOCKET_ENOTCONN') ? SOCKET_ENOTCONN : 107
));
}
}
}
33 changes: 27 additions & 6 deletions tests/FactoryStreamingClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ public function testWillRejectAndCloseAutomaticallyWhenAuthCommandReceivesErrorR
$this->logicalAnd(
$this->isInstanceOf('RuntimeException'),
$this->callback(function (\RuntimeException $e) {
return $e->getMessage() === 'Connection to redis://:***@localhost failed during AUTH command: ERR invalid password';
return $e->getMessage() === 'Connection to redis://:***@localhost failed during AUTH command: ERR invalid password (EACCES)';
}),
$this->callback(function (\RuntimeException $e) {
return $e->getCode() === (defined('SOCKET_EACCES') ? SOCKET_EACCES : 13);
}),
$this->callback(function (\RuntimeException $e) {
return $e->getPrevious()->getMessage() === 'ERR invalid password';
Expand Down Expand Up @@ -264,7 +267,10 @@ public function testWillRejectAndCloseAutomaticallyWhenSelectCommandReceivesErro
$this->logicalAnd(
$this->isInstanceOf('RuntimeException'),
$this->callback(function (\RuntimeException $e) {
return $e->getMessage() === 'Connection to redis://localhost/123 failed during SELECT command: ERR DB index is out of range';
return $e->getMessage() === 'Connection to redis://localhost/123 failed during SELECT command: ERR DB index is out of range (ENOENT)';
}),
$this->callback(function (\RuntimeException $e) {
return $e->getCode() === (defined('SOCKET_ENOENT') ? SOCKET_ENOENT : 2);
}),
$this->callback(function (\RuntimeException $e) {
return $e->getPrevious()->getMessage() === 'ERR DB index is out of range';
Expand All @@ -284,6 +290,9 @@ public function testWillRejectIfConnectorRejects()
$this->callback(function (\RuntimeException $e) {
return $e->getMessage() === 'Connection to redis://127.0.0.1:2 failed: Foo';
}),
$this->callback(function (\RuntimeException $e) {
return $e->getCode() === 42;
}),
$this->callback(function (\RuntimeException $e) {
return $e->getPrevious()->getMessage() === 'Foo';
})
Expand All @@ -299,7 +308,10 @@ public function testWillRejectIfTargetIsInvalid()
$this->logicalAnd(
$this->isInstanceOf('InvalidArgumentException'),
$this->callback(function (\InvalidArgumentException $e) {
return $e->getMessage() === 'Invalid Redis URI given';
return $e->getMessage() === 'Invalid Redis URI given (EINVAL)';
}),
$this->callback(function (\InvalidArgumentException $e) {
return $e->getCode() === (defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22);
})
)
));
Expand Down Expand Up @@ -399,7 +411,10 @@ public function testCancelWillRejectWithUriInMessageAndCancelConnectorWhenConnec
$this->logicalAnd(
$this->isInstanceOf('RuntimeException'),
$this->callback(function (\RuntimeException $e) use ($safe) {
return $e->getMessage() === 'Connection to ' . $safe . ' cancelled';
return $e->getMessage() === 'Connection to ' . $safe . ' cancelled (ECONNABORTED)';
}),
$this->callback(function (\RuntimeException $e) {
return $e->getCode() === (defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103);
})
)
));
Expand All @@ -420,7 +435,10 @@ public function testCancelWillCloseConnectionWhenConnectionWaitsForSelect()
$this->logicalAnd(
$this->isInstanceOf('RuntimeException'),
$this->callback(function (\RuntimeException $e) {
return $e->getMessage() === 'Connection to redis://127.0.0.1:2/123 cancelled';
return $e->getMessage() === 'Connection to redis://127.0.0.1:2/123 cancelled (ECONNABORTED)';
}),
$this->callback(function (\RuntimeException $e) {
return $e->getCode() === (defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103);
})
)
));
Expand All @@ -446,7 +464,10 @@ public function testCreateClientWithTimeoutParameterWillStartTimerAndRejectOnExp
$this->logicalAnd(
$this->isInstanceOf('RuntimeException'),
$this->callback(function (\Exception $e) {
return $e->getMessage() === 'Connection to redis://127.0.0.1:2?timeout=0 timed out after 0 seconds';
return $e->getMessage() === 'Connection to redis://127.0.0.1:2?timeout=0 timed out after 0 seconds (ETIMEDOUT)';
}),
$this->callback(function (\Exception $e) {
return $e->getCode() === (defined('SOCKET_ETIMEDOUT') ? SOCKET_ETIMEDOUT : 110);
})
)
));
Expand Down
12 changes: 11 additions & 1 deletion tests/LazyClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,17 @@ public function testPingAfterCloseWillRejectWithoutCreatingUnderlyingConnection(
$this->client->close();
$promise = $this->client->ping();

$promise->then(null, $this->expectCallableOnce());
$promise->then(null, $this->expectCallableOnceWith(
$this->logicalAnd(
$this->isInstanceOf('RuntimeException'),
$this->callback(function (\RuntimeException $e) {
return $e->getMessage() === 'Connection closed (ENOTCONN)';
}),
$this->callback(function (\RuntimeException $e) {
return $e->getCode() === (defined('SOCKET_ENOTCONN') ? SOCKET_ENOTCONN : 107);
})
)
));
}

public function testPingAfterPingWillNotStartIdleTimerWhenFirstPingResolves()
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy