Skip to content

Commit 0a316d5

Browse files
committed
Improve error reporting in examples and documentation
1 parent 1813c68 commit 0a316d5

16 files changed

+84
-38
lines changed

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ $client = new Clue\React\Docker\Client();
6666

6767
$client->imageSearch('clue')->then(function (array $images) {
6868
var_dump($images);
69+
}, function (Exception $e) {
70+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
6971
});
7072
```
7173

@@ -131,15 +133,17 @@ Please see the following section about [promises](#promises) for more details.
131133

132134
Sending requests is async (non-blocking), so you can actually send multiple requests in parallel.
133135
Docker will respond to each request with a response message, the order is not guaranteed.
134-
Sending requests uses a [Promise](https://github.com/reactphp/promise)-based interface that makes it easy to react to when a request is fulfilled (i.e. either successfully resolved or rejected with an error):
136+
Sending requests uses a [Promise](https://github.com/reactphp/promise)-based
137+
interface that makes it easy to react to when a command is completed
138+
(i.e. either successfully fulfilled or rejected with an error):
135139

136140
```php
137141
$client->version()->then(
138142
function ($result) {
139143
var_dump('Result received', $result);
140144
},
141-
function (Exception $error) {
142-
var_dump('There was an error', $error->getMessage());
145+
function (Exception $e) {
146+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
143147
}
144148
});
145149
```
@@ -214,10 +218,16 @@ the normal stream events:
214218

215219
```php
216220
$stream = $client->execStartStream($exec, $tty);
221+
217222
$stream->on('data', function ($data) {
218223
// data will be emitted in multiple chunk
219224
echo $data;
220225
});
226+
227+
$stream->on('error', function (Exception $e) {
228+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
229+
});
230+
221231
$stream->on('close', function () {
222232
// the stream just ended, this could(?) be a good thing
223233
echo 'Ended' . PHP_EOL;
@@ -233,9 +243,11 @@ Also note that this option has no effect if you execute with a TTY.
233243

234244
```php
235245
$stream = $client->execStartStream($exec, $tty, 'stderr');
246+
236247
$stream->on('data', function ($data) {
237248
echo 'STDOUT data: ' . $data;
238249
});
250+
239251
$stream->on('stderr', function ($data) {
240252
echo 'STDERR data: ' . $data;
241253
});
@@ -319,9 +331,9 @@ $client->imageCreate('clue/streamripper')->then(
319331
// $data is an array of *all* elements in the JSON stream
320332
var_dump($data);
321333
},
322-
function (Exception $error) {
334+
function (Exception $e) {
323335
// an error occurred (possibly after receiving *some* elements)
324-
echo 'Error: ' . $error->getMessage() . PHP_EOL;
336+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
325337
}
326338
);
327339
```
@@ -355,10 +367,16 @@ The resulting stream will emit the following events:
355367

356368
```php
357369
$stream = $client->imageCreateStream('clue/redis-benchmark');
370+
358371
$stream->on('data', function (array $data) {
359372
// data will be emitted for *each* complete element in the JSON stream
360373
echo $data['status'] . PHP_EOL;
361374
});
375+
376+
$stream->on('error', function (Exception $e) {
377+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
378+
});
379+
362380
$stream->on('close', function () {
363381
// the JSON stream just ended, this could(?) be a good thing
364382
echo 'Ended' . PHP_EOL;

examples/archive.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
});
2828
});
2929

30-
$tar->on('error', function ($e = null) {
30+
$tar->on('error', function (Exception $e) {
3131
// should not be invoked, unless the stream is somehow interrupted
32-
echo 'ERROR processing tar stream' . PHP_EOL . $e;
32+
echo 'Error in TAR stream: ' . $e->getMessage() . PHP_EOL;
3333
});
34-
$stream->on('error', function ($e = null) {
34+
35+
$stream->on('error', function (Exception $e) {
3536
// will be called if either parameter is invalid
36-
echo 'ERROR requesting stream' . PHP_EOL . $e;
37+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
3738
});
3839

3940
$stream->pipe($tar);

examples/attach-stream.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
$caret = new Clue\CaretNotation\Encoder("\t\r\n");
1818

1919
$stream = $client->containerAttachStream($container, true, true);
20+
2021
$stream->on('data', function ($data) use ($caret) {
2122
echo $caret->encode($data);
2223
});
2324

2425
$stream->on('error', function (Exception $e) {
2526
// will be called if either parameter is invalid
26-
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
27+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
2728
});
2829

2930
$stream->on('close', function () {

examples/benchmark-attach.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,23 @@
4545
$client->containerStart($container['Id'])->then(null, 'printf');
4646
});
4747

48-
$start = microtime(true);
4948
$bytes = 0;
5049
$stream->on('data', function ($chunk) use (&$bytes) {
5150
$bytes += strlen($chunk);
5251
});
5352

54-
$stream->on('error', 'printf');
53+
$stream->on('error', function (Exception $e) {
54+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
55+
});
5556

5657
// show stats when stream ends
58+
$start = microtime(true);
5759
$stream->on('close', function () use ($client, &$bytes, $start, $container) {
5860
$time = microtime(true) - $start;
5961
$client->containerRemove($container['Id'])->then(null, 'printf');
6062

6163
echo 'Received ' . $bytes . ' bytes in ' . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
6264
});
63-
}, 'printf');
65+
}, function (Exception $e) {
66+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
67+
});

examples/benchmark-exec.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
//
1414
// $ docker exec foo dd if=/dev/zero bs=1M count=1000 | dd of=/dev/null
1515

16-
1716
require __DIR__ . '/../vendor/autoload.php';
1817

1918
if (extension_loaded('xdebug')) {
@@ -33,18 +32,22 @@
3332
$client->execCreate($container, $cmd)->then(function ($info) use ($client) {
3433
$stream = $client->execStartStream($info['Id'], true);
3534

36-
$start = microtime(true);
3735
$bytes = 0;
3836
$stream->on('data', function ($chunk) use (&$bytes) {
3937
$bytes += strlen($chunk);
4038
});
4139

42-
$stream->on('error', 'printf');
40+
$stream->on('error', function (Exception $e) {
41+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
42+
});
4343

4444
// show stats when stream ends
45+
$start = microtime(true);
4546
$stream->on('close', function () use ($client, &$bytes, $start) {
4647
$time = microtime(true) - $start;
4748

4849
echo 'Received ' . $bytes . ' bytes in ' . round($time, 1) . 's => ' . round($bytes / $time / 1000000, 1) . ' MB/s' . PHP_EOL;
4950
});
50-
}, 'printf');
51+
}, function (Exception $e) {
52+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
53+
});

examples/events.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// this simple example displays all docker events that happen in the next 10s.
44
// try starting / removing a container in the meantime to see some output.
55

6-
76
require __DIR__ . '/../vendor/autoload.php';
87

98
$client = new Clue\React\Docker\Client();
@@ -22,4 +21,6 @@
2221
echo json_encode($event) . PHP_EOL;
2322
});
2423

25-
$stream->on('error', 'printf');
24+
$stream->on('error', function (Exception $e) {
25+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
26+
});

examples/exec-inspect.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,5 @@
3333
})->then(function ($info) {
3434
echo 'Inspected after execution: ' . json_encode($info, JSON_PRETTY_PRINT) . PHP_EOL;
3535
}, function (Exception $e) {
36-
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
37-
38-
if ($e instanceof React\Http\Message\ResponseException) {
39-
echo 'Response: ' . $e->getResponse()->getBody() . PHP_EOL;
40-
}
36+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
4137
});

examples/exec-stream.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,21 @@
4444
}
4545
});
4646

47-
$stream->on('error', 'printf');
47+
$stream->on('error', function (Exception $e) {
48+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
49+
});
4850

4951
// remember exit code of executed command once it closes
5052
$stream->on('close', function () use ($client, $info, &$exit) {
5153
$client->execInspect($info['Id'])->then(function ($info) use (&$exit) {
5254
$exit = $info['ExitCode'];
53-
}, 'printf');
55+
}, function (Exception $e) {
56+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
57+
});
5458
});
55-
}, 'printf');
59+
}, function (Exception $e) {
60+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
61+
});
5662

5763
Loop::run();
5864

examples/export.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
$stream = $client->containerExportStream($container);
1919

20-
$stream->on('error', function ($e = null) {
20+
$stream->on('error', function (Exception $e) {
2121
// will be called if the container is invalid/does not exist
22-
echo 'ERROR requesting stream' . PHP_EOL . $e;
22+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
2323
});
2424

25-
$out = new React\Stream\WritableResourceStream(fopen($target, 'w'));
25+
$out = new React\Stream\WritableResourceStream(fopen($target, 'w'));
2626
$stream->pipe($out);

examples/info.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88

99
$client->info()->then(function ($info) {
1010
echo json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
11-
}, 'printf');
11+
}, function (Exception $e) {
12+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
13+
});

0 commit comments

Comments
 (0)