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 13, 2023
1 parent 3cb4d16 commit 8fb6d3f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,19 @@ 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) {
throw $exception = new \RuntimeException('Unable to read stream contents: ' . $message);
});

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

return $contents;
}

/**
Expand Down

0 comments on commit 8fb6d3f

Please sign in to comment.