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 9cf0693
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 ($type, $message) use (&$exception) {
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 9cf0693

Please sign in to comment.