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 de7dcbb
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,23 @@ 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): *NEVER* given.
throw $exception = new \RuntimeException('Unable to read stream contents: ' . $message);
});

return $contents;
try {
return \stream_get_contents($this->stream);
} catch (\Throwable $e) {
if ($e === $exception) {
throw $e;
}

throw new \RuntimeException('Unable to read stream contents: ' . $e->getMessage(), 0, $e);
} finally {
\restore_error_handler();
}
}

/**
Expand Down

0 comments on commit de7dcbb

Please sign in to comment.