8000 Improve error reporting, include Redis URI in all connection errors · SimonFrings/reactphp-redis@2da3c60 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2da3c60

Browse files
committed
Improve error reporting, include Redis URI in all connection errors
1 parent 0c6a9b3 commit 2da3c60

File tree

7 files changed

+139
-54
lines changed

7 files changed

+139
-54
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ local Redis server and send some requests:
6161

6262
```php
6363
$factory = new Clue\React\Redis\Factory();
64+
$client = $factory->createLazyClient('localhost:6379');
6465

65-
$client = $factory->createLazyClient('localhost');
6666
$client->set('greeting', 'Hello world');
6767
$client->append('greeting', '!');
6868

@@ -125,7 +125,7 @@ It helps with establishing a plain TCP/IP or secure TLS connection to Redis
125125
and optionally authenticating (AUTH) and selecting the right database (SELECT).
126126

127127
```php
128-
$factory->createClient('redis://localhost:6379')->then(
128+
$factory->createClient('localhost:6379')->then(
129129
function (Client $client) {
130130
// client connected (and authenticated)
131131
},
@@ -146,7 +146,7 @@ reject its value with an Exception and will cancel the underlying TCP/IP
146146
connection attempt and/or Redis authentication.
147147

148148
```php
149-
$promise = $factory->createClient($redisUri);
149+
$promise = $factory->createClient($uri);
150150

151151
Loop::addTimer(3.0, function () use ($promise) {
152152
$promise->cancel();
@@ -222,7 +222,7 @@ It helps with establishing a plain TCP/IP or secure TLS connection to Redis
222222
and optionally authenticating (AUTH) and selecting the right database (SELECT).
223223

224224
```php
225-
$client = $factory->createLazyClient('redis://localhost:6379');
225+
$client = $factory->createLazyClient('localhost:6379');
226226

227227
$client->incr('hello');
228228
$client->end();

examples/cli.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
// $ php examples/cli.php
4+
// $ REDIS_URI=localhost:6379 php examples/cli.php
5+
36
use Clue\React\Redis\Client;
47
use Clue\React\Redis\Factory;
58
use React\EventLoop\Loop;
@@ -11,7 +14,7 @@
1114

1215
echo '# connecting to redis...' . PHP_EOL;
1316

14-
$factory->createClient('localhost')->then(function (Client $client) {
17+
$factory->createClient(getenv('REDIS_URI') ?: 'localhost:6379')->then(function (Client $client) {
1518
echo '# connected! Entering interactive mode, hit CTRL-D to quit' . PHP_EOL;
1619

1720
Loop::addReadStream(STDIN, function () use ($client) {
@@ -38,7 +41,7 @@
3841

3942
$promise->then(function ($data) {
4043
echo '# reply: ' . json_encode($data) . PHP_EOL;
41-
}, function ($e) {
44+
}, function (Exception $e) {
4245
echo '# error reply: ' . $e->getMessage() . PHP_EOL;
4346
});
4447
});
@@ -48,10 +51,7 @@
4851

4952
Loop::removeReadStream(STDIN);
5053
});
51-
}, function (Exception $error) {
52-
echo 'CONNECTION ERROR: ' . $error->getMessage() . PHP_EOL;
53-
if ($error->getPrevious()) {
54-
echo $error->getPrevious()->getMessage() . PHP_EOL;
55-
}
54+
}, function (Exception $e) {
55+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
5656
exit(1);
5757
});

examples/incr.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<?php
22

3+
// $ php examples/incr.php
4+
// $ REDIS_URI=localhost:6379 php examples/incr.php
5+
36
use Clue\React\Redis\Factory;
47

58
require __DIR__ . '/../vendor/autoload.php';
69

710
$factory = new Factory();
11+
$client = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
812

9-
$client = $factory->createLazyClient('localhost');
1013
$client->incr('test');
1114

1215
$client->get('test')->then(function ($result) {
1316
var_dump($result);
1417
}, function (Exception $e) {
1518
echo 'Error: ' . $e->getMessage() . PHP_EOL;
16-
if ($e->getPrevious()) {
17-
echo $e->getPrevious()->getMessage() . PHP_EOL;
18-
}
1919
exit(1);
2020
});
2121

22-
$client->end();
22+
//$client->end();

examples/publish.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<?php
22

3+
// $ php examples/publish.php
4+
// $ REDIS_URI=localhost:6379 php examples/publish.php channel message
5+
36
use Clue\React\Redis\Factory;
47

58
require __DIR__ . '/../vendor/autoload.php';
69

710
$factory = new Factory();
11+
$client = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
812

913
$channel = isset($argv[1]) ? $argv[1] : 'channel';
1014
$message = isset($argv[2]) ? $argv[2] : 'message';
1115

12-
$client = $factory->createLazyClient('localhost');
1316
$client->publish($channel, $message)->then(function ($received) {
1417
echo 'Successfully published. Received by ' . $received . PHP_EOL;
1518
}, function (Exception $e) {
1619
echo 'Unable to publish: ' . $e->getMessage() . PHP_EOL;
17-
if ($e->getPrevious()) {
18-
echo $e->getPrevious()->getMessage() . PHP_EOL;
19-
}
2020
exit(1);
2121
});
2222

examples/subscribe.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<?php
22

3+
// $ php examples/subscribe.php
4+
// $ REDIS_URI=localhost:6379 php examples/subscribe.php channel
5+
36
use Clue\React\Redis\Factory;
47
use React\EventLoop\Loop;
58

69
require __DIR__ . '/../vendor/autoload.php';
710

811
$factory = new Factory();
12+
$client = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
913

1014
$channel = isset($argv[1]) ? $argv[1] : 'channel';
1115

12-
$client = $factory->createLazyClient('localhost');
1316
$client->subscribe($channel)->then(function () {
1417
echo 'Now subscribed to channel ' . PHP_EOL;
1518
}, function (Exception $e) use ($client) {

src/Factory.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ public function createClient($uri)
5454
$parts = parse_url($uri);
5555
}
5656

57+
$uri = preg_replace(array('/(:)[^:\/]*(@)/', '/([?&]password=).*?($|&)/'), '$1***$2', $uri);
5758
if ($parts === false || !isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('redis', 'rediss', 'redis+unix'))) {
58-
return \React\Promise\reject(new \InvalidArgumentException('Given URL can not be parsed'));
59+
return \React\Promise\reject(new \InvalidArgumentException('Invalid Redis URI given'));
5960
}
6061

6162
$args = array();
@@ -70,9 +71,9 @@ public function createClient($uri)
7071
}
7172
$connecting = $this->connector->connect($authority);
7273

73-
$deferred = new Deferred(function ($_, $reject) use ($connecting) {
74+
$deferred = new Deferred(function ($_, $reject) use ($connecting, $uri) {
7475
// connection cancelled, start with rejecting attempt, then clean up
75-
$reject(new \RuntimeException('Connection to Redis server cancelled'));
76+
$reject(new \RuntimeException('Connection to ' . $uri . ' cancelled'));
7677

7778
// either close successful connection or cancel pending connection attempt
7879
$connecting->then(function (ConnectionInterface $connection) {
@@ -84,9 +85,9 @@ public function createClient($uri)
8485
$protocol = $this->protocol;
8586
$promise = $connecting->then(function (ConnectionInterface $stream) use ($protocol) {
8687
return new StreamingClient($stream, $protocol->createResponseParser(), $protocol->createSerializer());
87-
}, function (\Exception $e) {
88+
}, function (\Exception $e) use ($uri) {
8889
throw new \RuntimeException(
89-
'Connection to Redis server failed because underlying transport connection failed',
90+
'Connection to ' . $uri . ' failed: ' . $e->getMessage(),
9091
0,
9192
$e
9293
);
@@ -96,18 +97,18 @@ public function createClient($uri)
9697
$pass = isset($args['password']) ? $args['password'] : (isset($parts['pass']) ? rawurldecode($parts['pass']) : null);
9798
if (isset($args['password']) || isset($parts['pass'])) {
9899
$pass = isset($args['password']) ? $args['password'] : rawurldecode($parts['pass']);
99-
$promise = $promise->then(function (StreamingClient $client) use ($pass) {
100+
$promise = $promise->then(function (StreamingClient $client) use ($pass, $uri) {
100101
return $client->auth($pass)->then(
101102
function () use ($client) {
102103
return $client;
103104
},
104-
function ($error) use ($client) {
105+
function (\Exception $e) use ($client, $uri) {
105106
$client->close();
106107

107108
throw new \RuntimeException(
108-
'Connection to Redis server failed because AUTH command failed',
109+
'Connection to ' . $uri . ' failed during AUTH command: ' . $e->getMessage(),
109110
0,
110-
$error
111+
$e
111112
);
112113
}
113114
);
@@ -117,18 +118,18 @@ function ($error) use ($client) {
117118
// use `?db=1` query or `/1` path (skip first slash)
118119
if (isset($args['db']) || (isset($parts['path']) && $parts['path'] !== '/')) {
119120
$db = isset($args['db']) ? $args['db'] : substr($parts['path'], 1);
120-
$promise = $promise->then(function (StreamingClient $client) use ($db) {
121+
$promise = $promise->then(function (StreamingClient $client) use ($db, $uri) {
121122
return $client->select($db)->then(
122123
function () use ($client) {
123124
return $client;
124125
},
125-
function ($error) use ($client) {
126+
function (\Exception $e) use ($client, $uri) {
126127
$client->close();
127128

128129
throw new \RuntimeException(
129-
'Connection to Redis server failed because SELECT command failed',
130+
'Connection to ' . $uri . ' failed during SELECT command: ' . $e->getMessage(),
130131
0,
131-
$error
132+
$e
132133
);
133134
}
134135
);
@@ -143,10 +144,10 @@ function ($error) use ($client) {
143144
return $deferred->promise();
144145
}
145146

146-
return \React\Promise\Timer\timeout($deferred->promise(), $timeout, $this->loop)->then(null, function ($e) {
147+
return \React\Promise\Timer\timeout($deferred->promise(), $timeout, $this->loop)->then(null, function ($e) use ($uri) {
147148
if ($e instanceof TimeoutException) {
148149
throw new \RuntimeException(
149-
'Connection to Redis server timed out after ' . $e->getTimeout() . ' seconds'
150+
'Connection to ' . $uri . ' timed out after ' . $e->getTimeout() . ' seconds'
150151
);
151152
}
152153
throw $e;

0 commit comments

Comments
 (0)
0