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 5f80208
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,26 @@ 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 (string $type, string $message, string $file, int $line) use (&$exception): bool {

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(string, string, string, int): true given.
$exception = new \RuntimeException('Unable to read from stream: ' . $message);

return true;
});

$contents = '';

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;
Expand Down

0 comments on commit 5f80208

Please sign in to comment.