Skip to content

Commit

Permalink
Fix error handling in Stream::getContents()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Nov 10, 2023
1 parent 3cb4d16 commit ff5243c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,22 @@ public function getContents(): string
throw new \RuntimeException('Stream is detached');
}

if (false === $contents = @\stream_get_contents($this->stream)) {
throw new \RuntimeException('Unable to read stream contents: ' . (\error_get_last()['message'] ?? ''));
$exception = null;

set_error_handler(static function ($type, $message) use (&$exception) {

Check failure on line 265 in src/Stream.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $callback of function set_error_handler expects (callable(int, string, string, int): bool)|null, Closure(mixed, mixed): void given.
$exception = new \RuntimeException('Unable to read from stream: ' . $message);
});

try {
$contents = stream_get_contents($this->stream);
} catch (\Throwable $e) {
$exception = new \RuntimeException('Unable to read from stream: ' . $e->getMessage(), 0, $e);
}

restore_error_handler();

if ($exception) {
throw $exception;
}

return $contents;

Check failure on line 281 in src/Stream.php

View workflow job for this annotation

GitHub Actions / PHPStan

Variable $contents might not be defined.
Expand Down

0 comments on commit ff5243c

Please sign in to comment.