Skip to content

Commit 4b21ae4

Browse files
committed
Emit JsonProgressException for progress messages with an "error" key
1 parent c0b199a commit 4b21ae4

4 files changed

Lines changed: 62 additions & 0 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ $client->imageCreate('clue/streamripper')->then(
174174
},
175175
function ($error) {
176176
// an error occurred (possibly after receiving *some* elements)
177+
178+
if ($error instanceof Io\JsonProgressException) {
179+
// a progress message (usually the last) contains an error message
180+
} else {
181+
// any other error, like invalid request etc.
182+
}
177183
}
178184
);
179185
```
@@ -199,6 +205,8 @@ The resulting stream will emit the following events:
199205

200206
* `progress`: for *each* element in the update stream
201207
* `error`: once if an error occurs, will close() stream then
208+
* Will emit an [`Io\JsonProgressException`](#jsonprogressexception) if an individual progress message contains an error message
209+
* Any other `Exception` in case of an transport error, like invalid request etc.
202210
* `close`: once the stream ends (either finished or after "error")
203211

204212
Please note that the resulting stream does not emit any "data" events, so
@@ -218,6 +226,13 @@ $stream->on('close', function () {
218226

219227
See also the [pull example](examples/pull.php) and the [push example](examples/push.php).
220228

229+
### JsonProgressException
230+
231+
The `Io\JsonProgressException` will be thrown by [JSON streaming](#json-streaming)
232+
endpoints if an individual progress message contains an error message.
233+
234+
The `getData()` method can be used to obtain the progress message.
235+
221236
## Install
222237

223238
The recommended way to install this library is [through composer](http://getcomposer.org). [New to composer?](http://getcomposer.org/doc/00-intro.md)

src/Io/JsonProgressException.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Clue\React\Docker\Io;
4+
5+
use RuntimeException;
6+
7+
/**
8+
* Will be used for JSON streaming endpoints where an individal progress event contains an "error" key
9+
*/
10+
class JsonProgressException extends RuntimeException
11+
{
12+
private $data;
13+
14+
public function __construct($data, $message = null, $code = null, $previous = null)
15+
{
16+
if ($message === null) {
17+
$message = $data['error'];
18+
}
19+
parent::__construct($message, $code, $previous);
20+
$this->data = $data;
21+
}
22+
23+
public function getData()
24+
{
25+
return $data;
26+
}
27+
}

src/Io/StreamingParser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public function parseJsonStream(PromiseInterface $promise)
3232
$objects = $parser->push($data);
3333

3434
foreach ($objects as $object) {
35+
if (isset($object['error'])) {
36+
$out->emit('error', array(new JsonProgressException($object), $out));
37+
$out->close();
38+
return;
39+
}
3540
$out->emit('progress', array($object, $out));
3641
}
3742
});

tests/FunctionalClientTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ public function testImageTag()
8484
$ret = $this->waitFor($promise, $this->loop);
8585
}
8686

87+
public function testImageCreateStreamMissingWillEmitJsonError()
88+
{
89+
$stream = $this->client->imageCreateStream('clue/does-not-exist');
90+
91+
// one "progress" event, but no "data" events
92+
$stream->on('progress', $this->expectCallableOnce());
93+
$stream->on('data', $this->expectCallableNever());
94+
95+
// will emit "error" with JsonProgressException and close
96+
$stream->on('error', $this->expectCallableOnceParameter('Clue\React\Docker\Io\JsonProgressException'));
97+
$stream->on('close', $this->expectCallableOnce());
98+
99+
$this->loop->run();
100+
}
101+
87102
public function testInfo()
88103
{
89104
$this->expectPromiseResolve($this->client->info());

0 commit comments

Comments
 (0)