Skip to content

Commit 35095e4

Browse files
committed
Forward parser Exception as a Client error (closes #4)
1 parent 48d2fe2 commit 35095e4

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/Client.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Clue\React\Ami\Protocol\Parser;
1010
use React\Promise\Deferred;
1111
use Exception;
12+
use UnexpectedValueException;
1213
use Clue\React\Ami\Protocol\Message;
1314
use Clue\React\Ami\Protocol\ErrorException;
1415

@@ -28,11 +29,20 @@ public function __construct(StreamInterface $stream, Parser $parser = null)
2829

2930
$that = $this;
3031
$this->stream->on('data', function ($chunk) use ($parser, $that) {
31-
foreach ($parser->push($chunk) as $message) {
32+
try {
33+
$messages = $parser->push($chunk);
34+
} catch (UnexpectedValueException $e) {
35+
$that->emit('error', array($e, $this));
36+
return;
37+
}
38+
39+
foreach ($messages as $message) {
3240
$that->handleMessage($message);
3341
}
3442
});
3543

44+
$this->on('error', array($that, 'close'));
45+
3646
$this->stream->on('close', array ($that, 'close'));
3747

3848
$this->stream->resume();

tests/ClientTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,17 @@ public function testClosingStreamClosesClient()
1818
$stream->close();
1919
//$stream->emit('close', array($this));
2020
}
21+
22+
public function testParserExceptionForwardsErrorAndClosesClient()
23+
{
24+
$stream = new ThroughStream();
25+
$parser = new Parser();
26+
27+
$client = new Client($stream, $parser);
28+
29+
$client->on('error', $this->expectCallableOnce());
30+
$client->on('close', $this->expectCallableOnce());
31+
32+
$stream->emit('data', array("invalid chunk\r\n\r\ninvalid chunk\r\n\r\n"));
33+
}
2134
}

0 commit comments

Comments
 (0)