From 922e0327a6b36d1da0acca69993ac85395adcea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Granado?= Date: Thu, 30 Nov 2023 16:49:52 +0100 Subject: [PATCH] feat: adds NotFoundException when file does not exit (#853) * feat: adds NotFoundException when file does not exit * keep InvalidArgumentException compatibility --- src/Exception/NotFoundException.php | 19 +++++++++++++++++++ src/File/Loader.php | 7 +++++-- tests/tests/Image/AbstractImagineTest.php | 2 +- .../Image/Metadata/MetadataReaderTestCase.php | 2 +- 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/Exception/NotFoundException.php diff --git a/src/Exception/NotFoundException.php b/src/Exception/NotFoundException.php new file mode 100644 index 000000000..b72b0317c --- /dev/null +++ b/src/Exception/NotFoundException.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Imagine\Exception; + +/** + * Imagine-specific invalid not found exception. + */ +class NotFoundException extends InvalidArgumentException +{ +} diff --git a/src/File/Loader.php b/src/File/Loader.php index 9294194b8..af29b9440 100644 --- a/src/File/Loader.php +++ b/src/File/Loader.php @@ -9,6 +9,7 @@ namespace Imagine\File; use Imagine\Exception\InvalidArgumentException; +use Imagine\Exception\NotFoundException; use Imagine\Exception\RuntimeException; /** @@ -157,11 +158,12 @@ protected function readLocalFile() * Check that the file exists and it's readable. * * @throws \Imagine\Exception\InvalidArgumentException + * @throws \Imagine\Exception\NotFoundException */ protected function checkLocalFile() { if (!is_file($this->path)) { - throw new InvalidArgumentException(sprintf('File %s does not exist.', $this->path)); + throw new NotFoundException(sprintf('File %s does not exist.', $this->path)); } if (!is_readable($this->path)) { throw new InvalidArgumentException(sprintf('File %s is not readable.', $this->path)); @@ -212,6 +214,7 @@ protected function isCurlSupported() * Read a remote file using the cURL extension. * * @throws \Imagine\Exception\InvalidArgumentException + * @throws \Imagine\Exception\NotFoundException * * @return string */ @@ -239,7 +242,7 @@ protected function readRemoteFileWithCurl() $responseInfo = curl_getinfo($curl); curl_close($curl); if ($responseInfo['http_code'] == 404) { - throw new InvalidArgumentException(sprintf('File %s does not exist.', $this->path)); + throw new NotFoundException(sprintf('File %s does not exist.', $this->path)); } if ($responseInfo['http_code'] < 200 || $responseInfo['http_code'] >= 300) { throw new InvalidArgumentException(sprintf('Failed to download "%s": %s', $this->path, $responseInfo['http_code'])); diff --git a/tests/tests/Image/AbstractImagineTest.php b/tests/tests/Image/AbstractImagineTest.php index c14427ffc..7dc5bd641 100644 --- a/tests/tests/Image/AbstractImagineTest.php +++ b/tests/tests/Image/AbstractImagineTest.php @@ -145,7 +145,7 @@ public function testShouldFailOnUnknownImage() { $invalidResource = __DIR__ . '/path/that/does/not/exist'; - $this->isGoingToThrowException('Imagine\Exception\InvalidArgumentException', sprintf('File %s does not exist.', $invalidResource)); + $this->isGoingToThrowException('Imagine\Exception\NotFoundException', sprintf('File %s does not exist.', $invalidResource)); $this->getImagine()->open($invalidResource); } diff --git a/tests/tests/Image/Metadata/MetadataReaderTestCase.php b/tests/tests/Image/Metadata/MetadataReaderTestCase.php index 25b7c5026..1cfa51e34 100644 --- a/tests/tests/Image/Metadata/MetadataReaderTestCase.php +++ b/tests/tests/Image/Metadata/MetadataReaderTestCase.php @@ -50,7 +50,7 @@ public function testReadFromHttpFile() public function testReadFromInvalidFileThrowsAnException() { - $this->isGoingToThrowException('Imagine\Exception\InvalidArgumentException', 'File /path/to/no/file does not exist.'); + $this->isGoingToThrowException('Imagine\Exception\NotFoundException', 'File /path/to/no/file does not exist.'); $this->getReader()->readFile('/path/to/no/file'); }