Skip to content

Commit

Permalink
Make more clear exception messages with quotes (#179)
Browse files Browse the repository at this point in the history
* Make more clear exception messages with quotes

* cs

* Fixed tests
  • Loading branch information
Nyholm committed Jun 19, 2021
1 parent a4a3629 commit 2dc58be
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Factory/Psr17Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public function createStreamFromFile(string $filename, string $mode = 'r'): Stre
try {
$resource = @\fopen($filename, $mode);
} catch (\Throwable $e) {
throw new \RuntimeException('The file ' . $filename . ' cannot be opened.');
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
}

if (false === $resource) {
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.');
throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode));
}

throw new \RuntimeException('The file ' . $filename . ' cannot be opened.');
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $filename));
}

return Stream::create($resource);
Expand Down
2 changes: 1 addition & 1 deletion src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function seek($offset, $whence = \SEEK_SET): void
}

if (-1 === \fseek($this->stream, $offset, $whence)) {
throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, true));
throw new \RuntimeException('Unable to seek to stream position "' . $offset . '" with whence ' . \var_export($whence, true));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getStream(): StreamInterface
try {
return Stream::create(\fopen($this->file, 'r'));
} catch (\Throwable $e) {
throw new \RuntimeException('The file ' . $this->file . ' cannot be opened.');
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $this->file));
}
}

Expand All @@ -141,7 +141,7 @@ public function moveTo($targetPath): void
// Copy the contents of a stream into another stream until end-of-file.
$dest = Stream::create(\fopen($targetPath, 'w'));
} catch (\Throwable $e) {
throw new \RuntimeException('The file ' . $targetPath . ' cannot be opened.');
throw new \RuntimeException(\sprintf('The file "%s" cannot be opened.', $targetPath));
}

while (!$stream->eof()) {
Expand All @@ -154,7 +154,7 @@ public function moveTo($targetPath): void
}

if (false === $this->moved) {
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath));
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s"', $targetPath));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(string $uri = '')
{
if ('' !== $uri) {
if (false === $parts = \parse_url($uri)) {
throw new \InvalidArgumentException("Unable to parse URI: $uri");
throw new \InvalidArgumentException(\sprintf('Unable to parse URI: "%s"', $uri));
}

// Apply parse_url parts to a URI.
Expand Down
2 changes: 1 addition & 1 deletion tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testRequestUriMayBeUri()
public function testValidateRequestUri()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unable to parse URI: ///');
$this->expectExceptionMessage('Unable to parse URI: "///"');

new Request('GET', '///');
}
Expand Down

0 comments on commit 2dc58be

Please sign in to comment.