Skip to content

Commit

Permalink
feat: adds NotFoundException when file does not exit (#853)
Browse files Browse the repository at this point in the history
* feat: adds NotFoundException when file does not exit

* keep InvalidArgumentException compatibility
  • Loading branch information
playmono authored Nov 30, 2023
1 parent c4ca147 commit 922e032
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/Exception/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <[email protected]>
*
* 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
{
}
7 changes: 5 additions & 2 deletions src/File/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Imagine\File;

use Imagine\Exception\InvalidArgumentException;
use Imagine\Exception\NotFoundException;
use Imagine\Exception\RuntimeException;

/**
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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']));
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/Image/AbstractImagineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/tests/Image/Metadata/MetadataReaderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down

0 comments on commit 922e032

Please sign in to comment.