Skip to content

Improve error reporting by appending previous exception messages #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ function buffer(ReadableStreamInterface $stream, $maxLength = null)

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

$stream->on('error', function ($error) use ($reject) {
$reject(new \RuntimeException('An error occured on the underlying stream while buffering', 0, $error));
$stream->on('error', function (\Exception $e) use ($reject) {
$reject(new \RuntimeException(
'An error occured on the underlying stream while buffering: ' . $e->getMessage(),
$e->getCode(),
$e
));
});

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

return $promise->then(null, function ($error) use (&$buffer, $bufferer, $stream) {
return $promise->then(null, function (\Exception $error) use (&$buffer, $bufferer, $stream) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be Throwable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? In case of a TypeError for example, it wont remove the listener here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The promise is created a few lines above and only ever rejected with a RuntimeException, hence there's no need to check for Throwable here. Using a Throwable probably wouldn't hurt either in the long run once this targets only newer PHP versions, but at the moment nothing is gained by using Throwable.

// promise rejected => clear buffer and buffering
$buffer = '';
$stream->removeListener('data', $bufferer);
Expand Down Expand Up @@ -140,9 +144,13 @@ function first(EventEmitterInterface $stream, $event = 'data')
$stream->on($event, $listener);

if ($event !== 'error') {
$stream->on('error', function ($error) use ($stream, $event, $listener, $reject) {
$stream->on('error', function (\Exception $e) use ($stream, $event, $listener, $reject) {
$stream->removeListener($event, $listener);
$reject(new \RuntimeException('An error occured on the underlying stream while waiting for event', 0, $error));
$reject(new \RuntimeException(
'An error occured on the underlying stream while waiting for event: ' . $e->getMessage(),
$e->getCode(),
$e
));
});
}

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

$promise = new Promise\Promise(function ($resolve, $reject) use ($stream, &$buffer) {
$stream->on('error', function ($error) use ($reject) {
$reject(new \RuntimeException('An error occured on the underlying stream while buffering', 0, $error));
$stream->on('error', function (\Exception $e) use ($reject) {
$reject(new \RuntimeException(
'An error occured on the underlying stream while buffering: ' . $e->getMessage(),
$e->getCode(),
$e
));
});

$stream->on('close', function () use ($resolve, &$buffer) {
Expand Down
16 changes: 12 additions & 4 deletions tests/AllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ public function testEmittingErrorOnStreamRejects()
$stream = new ThroughStream();
$promise = Stream\all($stream);

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

$this->expectPromiseReject($promise);
$promise->then(null, $this->expectCallableOnceWith(new \RuntimeException(
'An error occured on the underlying stream while buffering: test',
42,
new \RuntimeException('test', 42)
)));
}

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

$stream->emit('data', array('hello', $stream));
$stream->emit('error', array(new \RuntimeException('test')));
$stream->emit('error', array(new \RuntimeException('test', 42)));

$this->expectPromiseReject($promise);
$promise->then(null, $this->expectCallableOnceWith(new \RuntimeException(
'An error occured on the underlying stream while buffering: test',
42,
new \RuntimeException('test', 42)
)));
}

public function testCancelPendingStreamWillReject()
Expand Down
16 changes: 12 additions & 4 deletions tests/BufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ public function testEmittingErrorOnStreamRejects()
$stream = new ThroughStream();
$promise = Stream\buffer($stream);

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

$this->expectPromiseReject($promise);
$promise->then(null, $this->expectCallableOnceWith(new \RuntimeException(
'An error occured on the underlying stream while buffering: test',
42,
new \RuntimeException('test', 42)
)));
}

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

$stream->emit('data', array('hello', $stream));
$stream->emit('error', array(new \RuntimeException('test')));
$stream->emit('error', array(new \RuntimeException('test', 42)));

$this->expectPromiseReject($promise);
$promise->then(null, $this->expectCallableOnceWith(new \RuntimeException(
'An error occured on the underlying stream while buffering: test',
42,
new \RuntimeException('test', 42)
)));
}

public function testCancelPendingStreamWillReject()
Expand Down
8 changes: 6 additions & 2 deletions tests/FirstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ public function testEmittingErrorOnStreamWillReject()
$stream = new ThroughStream();
$promise = Stream\first($stream);

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

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

public function testEmittingErrorResolvesWhenWaitingForErrorEvent()
Expand Down
13 changes: 1 addition & 12 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,7 @@ protected function expectCallableOnceWith($value)
$mock
->expects($this->once())
->method('__invoke')
->with($this->equalTo($value));

return $mock;
}

protected function expectCallableOnceParameter($type)
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($this->isInstanceOf($type));
->with($value);

return $mock;
}
Expand Down