Skip to content

Commit eefd5c2

Browse files
authored
Merge pull request #116 from clue-labs/errors
Improve error reporting, include Redis URI and socket error codes in all connection errors
2 parents 408a248 + af0cce3 commit eefd5c2

File tree

11 files changed

+562
-169
lines changed

11 files changed

+562
-169
lines changed

README.md

Lines changed: 6 additions & 6 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

@@ -118,14 +118,14 @@ $factory = new Clue\React\Redis\Factory(null, $connector);
118118

119119
#### createClient()
120120

121-
The `createClient(string $redisUri): PromiseInterface<Client,Exception>` method can be used to
121+
The `createClient(string $uri): PromiseInterface<Client,Exception>` method can be used to
122122
create a new [`Client`](#client).
123123

124124
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();
@@ -215,14 +215,14 @@ $factory->createClient('localhost?timeout=0.5');
215215

216216
#### createLazyClient()
217217

218-
The `createLazyClient(string $redisUri): Client` method can be used to
218+
The `createLazyClient(string $uri): Client` method can be used to
219219
create a new [`Client`](#client).
220220

221221
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) {

0 commit comments

Comments
 (0)