Skip to content

Commit 83a384e

Browse files
Improve error handling (#185)
1 parent 2212385 commit 83a384e

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed

src/Factory/Psr17Factory.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,16 @@ public function createStream(string $content = ''): StreamInterface
3737

3838
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
3939
{
40-
try {
41-
$resource = @\fopen($filename, $mode);
42-
} catch (\Throwable $e) {
43-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
40+
if ('' === $filename) {
41+
throw new \RuntimeException('Path cannot be empty');
4442
}
4543

46-
if (false === $resource) {
44+
if (false === $resource = @\fopen($filename, $mode)) {
4745
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
4846
throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode));
4947
}
5048

51-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
49+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $filename, \error_get_last()['message'] ?? ''));
5250
}
5351

5452
return Stream::create($resource);

src/Stream.php

+26-10
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,20 @@ public function getSize(): ?int
185185

186186
public function tell(): int
187187
{
188-
if (false === $result = \ftell($this->stream)) {
189-
throw new \RuntimeException('Unable to determine stream position');
188+
if (!isset($this->stream)) {
189+
throw new \RuntimeException('Stream is detached');
190+
}
191+
192+
if (false === $result = @\ftell($this->stream)) {
193+
throw new \RuntimeException('Unable to determine stream position: ' . (\error_get_last()['message'] ?? ''));
190194
}
191195

192196
return $result;
193197
}
194198

195199
public function eof(): bool
196200
{
197-
return !$this->stream || \feof($this->stream);
201+
return !isset($this->stream) || \feof($this->stream);
198202
}
199203

200204
public function isSeekable(): bool
@@ -204,6 +208,10 @@ public function isSeekable(): bool
204208

205209
public function seek($offset, $whence = \SEEK_SET): void
206210
{
211+
if (!isset($this->stream)) {
212+
throw new \RuntimeException('Stream is detached');
213+
}
214+
207215
if (!$this->seekable) {
208216
throw new \RuntimeException('Stream is not seekable');
209217
}
@@ -225,15 +233,19 @@ public function isWritable(): bool
225233

226234
public function write($string): int
227235
{
236+
if (!isset($this->stream)) {
237+
throw new \RuntimeException('Stream is detached');
238+
}
239+
228240
if (!$this->writable) {
229241
throw new \RuntimeException('Cannot write to a non-writable stream');
230242
}
231243

232244
// We can't know the size after writing anything
233245
$this->size = null;
234246

235-
if (false === $result = \fwrite($this->stream, $string)) {
236-
throw new \RuntimeException('Unable to write to stream');
247+
if (false === $result = @\fwrite($this->stream, $string)) {
248+
throw new \RuntimeException('Unable to write to stream: ' . (\error_get_last()['message'] ?? ''));
237249
}
238250

239251
return $result;
@@ -246,12 +258,16 @@ public function isReadable(): bool
246258

247259
public function read($length): string
248260
{
261+
if (!isset($this->stream)) {
262+
throw new \RuntimeException('Stream is detached');
263+
}
264+
249265
if (!$this->readable) {
250266
throw new \RuntimeException('Cannot read from non-readable stream');
251267
}
252268

253-
if (false === $result = \fread($this->stream, $length)) {
254-
throw new \RuntimeException('Unable to read from stream');
269+
if (false === $result = @\fread($this->stream, $length)) {
270+
throw new \RuntimeException('Unable to read from stream: ' . (\error_get_last()['message'] ?? ''));
255271
}
256272

257273
return $result;
@@ -260,11 +276,11 @@ public function read($length): string
260276
public function getContents(): string
261277
{
262278
if (!isset($this->stream)) {
263-
throw new \RuntimeException('Unable to read stream contents');
279+
throw new \RuntimeException('Stream is detached');
264280
}
265281

266-
if (false === $contents = \stream_get_contents($this->stream)) {
267-
throw new \RuntimeException('Unable to read stream contents');
282+
if (false === $contents = @\stream_get_contents($this->stream)) {
283+
throw new \RuntimeException('Unable to read stream contents: ' . (\error_get_last()['message'] ?? ''));
268284
}
269285

270286
return $contents;

src/UploadedFile.php

+14-15
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __construct($streamOrFile, $size, $errorStatus, $clientFilename
8080

8181
if (\UPLOAD_ERR_OK === $this->error) {
8282
// Depending on the value set file or stream variable.
83-
if (\is_string($streamOrFile)) {
83+
if (\is_string($streamOrFile) && '' !== $streamOrFile) {
8484
$this->file = $streamOrFile;
8585
} elseif (\is_resource($streamOrFile)) {
8686
$this->stream = Stream::create($streamOrFile);
@@ -114,11 +114,11 @@ public function getStream(): StreamInterface
114114
return $this->stream;
115115
}
116116

117-
try {
118-
return Stream::create(\fopen($this->file, 'r'));
119-
} catch (\Throwable $e) {
120-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $this->file));
117+
if (false === $resource = @\fopen($this->file, 'r')) {
118+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $this->file, \error_get_last()['message'] ?? ''));
121119
}
120+
121+
return Stream::create($resource);
122122
}
123123

124124
public function moveTo($targetPath): void
@@ -130,20 +130,23 @@ public function moveTo($targetPath): void
130130
}
131131

132132
if (null !== $this->file) {
133-
$this->moved = 'cli' === \PHP_SAPI ? \rename($this->file, $targetPath) : \move_uploaded_file($this->file, $targetPath);
133+
$this->moved = 'cli' === \PHP_SAPI ? @\rename($this->file, $targetPath) : @\move_uploaded_file($this->file, $targetPath);
134+
135+
if (false === $this->moved) {
136+
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, \error_get_last()['message'] ?? ''));
137+
}
134138
} else {
135139
$stream = $this->getStream();
136140
if ($stream->isSeekable()) {
137141
$stream->rewind();
138142
}
139143

140-
try {
141-
// Copy the contents of a stream into another stream until end-of-file.
142-
$dest = Stream::create(\fopen($targetPath, 'w'));
143-
} catch (\Throwable $e) {
144-
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $targetPath));
144+
if (false === $resource = @\fopen($targetPath, 'w')) {
145+
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $targetPath, \error_get_last()['message'] ?? ''));
145146
}
146147

148+
$dest = Stream::create($resource);
149+
147150
while (!$stream->eof()) {
148151
if (!$dest->write($stream->read(1048576))) {
149152
break;
@@ -152,10 +155,6 @@ public function moveTo($targetPath): void
152155

153156
$this->moved = true;
154157
}
155-
156-
if (false === $this->moved) {
157-
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s"', $targetPath));
158-
}
159158
}
160159

161160
public function getSize(): int

0 commit comments

Comments
 (0)