Skip to content

Commit c6e443f

Browse files
committed
Improve error reporting by appending previous exception messages
1 parent 09c4500 commit c6e443f

File tree

5 files changed

+50
-29
lines changed

5 files changed

+50
-29
lines changed

src/functions.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ function buffer(ReadableStreamInterface $stream, $maxLength = null)
6767

6868
$stream->on('data', $bufferer);
6969

70-
$stream->on('error', function ($error) use ($reject) {
71-
$reject(new \RuntimeException('An error occured on the underlying stream while buffering', 0, $error));
70+
$stream->on('error', function (\Exception $e) use ($reject) {
71+
$reject(new \RuntimeException(
72+
'An error occured on the underlying stream while buffering: ' . $e->getMessage(),
73+
$e->getCode(),
74+
$e
75+
));
7276
});
7377

7478
$stream->on('close', function () use ($resolve, &$buffer) {
@@ -78,7 +82,7 @@ function buffer(ReadableStreamInterface $stream, $maxLength = null)
7882
$reject(new \RuntimeException('Cancelled buffering'));
7983
});
8084

81-
return $promise->then(null, function ($error) use (&$buffer, $bufferer, $stream) {
85+
return $promise->then(null, function (\Exception $error) use (&$buffer, $bufferer, $stream) {
8286
// promise rejected => clear buffer and buffering
8387
$buffer = '';
8488
$stream->removeListener('data', $bufferer);
@@ -140,9 +144,13 @@ function first(EventEmitterInterface $stream, $event = 'data')
140144
$stream->on($event, $listener);
141145

142146
if ($event !== 'error') {
143-
$stream->on('error', function ($error) use ($stream, $event, $listener, $reject) {
147+
$stream->on('error', function (\Exception $e) use ($stream, $event, $listener, $reject) {
144148
$stream->removeListener($event, $listener);
145-
$reject(new \RuntimeException('An error occured on the underlying stream while waiting for event', 0, $error));
149+
$reject(new \RuntimeException(
150+
'An error occured on the underlying stream while waiting for event: ' . $e->getMessage(),
151+
$e->getCode(),
152+
$e
153+
));
146154
});
147155
}
148156

@@ -207,8 +215,12 @@ function all(EventEmitterInterface $stream, $event = 'data')
207215
$stream->on($event, $bufferer);
208216

209217
$promise = new Promise\Promise(function ($resolve, $reject) use ($stream, &$buffer) {
210-
$stream->on('error', function ($error) use ($reject) {
211-
$reject(new \RuntimeException('An error occured on the underlying stream while buffering', 0, $error));
218+
$stream->on('error', function (\Exception $e) use ($reject) {
219+
$reject(new \RuntimeException(
220+
'An error occured on the underlying stream while buffering: ' . $e->getMessage(),
221+
$e->getCode(),
222+
$e
223+
));
212224
});
213225

214226
$stream->on('close', function () use ($resolve, &$buffer) {

tests/AllTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ public function testEmittingErrorOnStreamRejects()
8686
$stream = new ThroughStream();
8787
$promise = Stream\all($stream);
8888

89-
$stream->emit('error', array(new \RuntimeException('test')));
89+
$stream->emit('error', array(new \RuntimeException('test', 42)));
9090

91-
$this->expectPromiseReject($promise);
91+
$promise->then(null, $this->expectCallableOnceWith(new \RuntimeException(
92+
'An error occured on the underlying stream while buffering: test',
93+
42,
94+
new \RuntimeException('test', 42)
95+
)));
9296
}
9397

9498
public function testEmittingErrorAfterEmittingDataOnStreamRejects()
@@ -97,9 +101,13 @@ public function testEmittingErrorAfterEmittingDataOnStreamRejects()
97101
$promise = Stream\all($stream);
98102

99103
$stream->emit('data', array('hello', $stream));
100-
$stream->emit('error', array(new \RuntimeException('test')));
104+
$stream->emit('error', array(new \RuntimeException('test', 42)));
101105

102-
$this->expectPromiseReject($promise);
106+
$promise->then(null, $this->expectCallableOnceWith(new \RuntimeException(
107+
'An error occured on the underlying stream while buffering: test',
108+
42,
109+
new \RuntimeException('test', 42)
110+
)));
103111
}
104112

105113
public function testCancelPendingStreamWillReject()

tests/BufferTest.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ public function testEmittingErrorOnStreamRejects()
5555
$stream = new ThroughStream();
5656
$promise = Stream\buffer($stream);
5757

58-
$stream->emit('error', array(new \RuntimeException('test')));
58+
$stream->emit('error', array(new \RuntimeException('test', 42)));
5959

60-
$this->expectPromiseReject($promise);
60+
$promise->then(null, $this->expectCallableOnceWith(new \RuntimeException(
61+
'An error occured on the underlying stream while buffering: test',
62+
42,
63+
new \RuntimeException('test', 42)
64+
)));
6165
}
6266

6367
public function testEmittingErrorAfterEmittingDataOnStreamRejects()
@@ -66,9 +70,13 @@ public function testEmittingErrorAfterEmittingDataOnStreamRejects()
6670
$promise = Stream\buffer($stream);
6771

6872
$stream->emit('data', array('hello', $stream));
69-
$stream->emit('error', array(new \RuntimeException('test')));
73+
$stream->emit('error', array(new \RuntimeException('test', 42)));
7074

71-
$this->expectPromiseReject($promise);
75+
$promise->then(null, $this->expectCallableOnceWith(new \RuntimeException(
76+
'An error occured on the underlying stream while buffering: test',
77+
42,
78+
new \RuntimeException('test', 42)
79+
)));
7280
}
7381

7482
public function testCancelPendingStreamWillReject()

tests/FirstTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ public function testEmittingErrorOnStreamWillReject()
8383
$stream = new ThroughStream();
8484
$promise = Stream\first($stream);
8585

86-
$stream->emit('error', array(new \RuntimeException('test')));
86+
$stream->emit('error', array(new \RuntimeException('test', 42)));
8787

88-
$this->expectPromiseReject($promise);
88+
$promise->then(null, $this->expectCallableOnceWith(new \RuntimeException(
89+
'An error occured on the underlying stream while waiting for event: test',
90+
42,
91+
new \RuntimeException('test', 42)
92+
)));
8993
}
9094

9195
public function testEmittingErrorResolvesWhenWaitingForErrorEvent()

tests/TestCase.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,7 @@ protected function expectCallableOnceWith($value)
2222
$mock
2323
->expects($this->once())
2424
->method('__invoke')
25-
->with($this->equalTo($value));
26-
27-
return $mock;
28-
}
29-
30-
protected function expectCallableOnceParameter($type)
31-
{
32-
$mock = $this->createCallableMock();
33-
$mock
34-
->expects($this->once())
35-
->method('__invoke')
36-
->with($this->isInstanceOf($type));
25+
->with($value);
3726

3827
return $mock;
3928
}

0 commit comments

Comments
 (0)