Skip to content

Commit 922e032

Browse files
authored
feat: adds NotFoundException when file does not exit (#853)
* feat: adds NotFoundException when file does not exit * keep InvalidArgumentException compatibility
1 parent c4ca147 commit 922e032

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

src/Exception/NotFoundException.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Imagine package.
5+
*
6+
* (c) Bulat Shakirzyanov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Imagine\Exception;
13+
14+
/**
15+
* Imagine-specific invalid not found exception.
16+
*/
17+
class NotFoundException extends InvalidArgumentException
18+
{
19+
}

src/File/Loader.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Imagine\File;
1010

1111
use Imagine\Exception\InvalidArgumentException;
12+
use Imagine\Exception\NotFoundException;
1213
use Imagine\Exception\RuntimeException;
1314

1415
/**
@@ -157,11 +158,12 @@ protected function readLocalFile()
157158
* Check that the file exists and it's readable.
158159
*
159160
* @throws \Imagine\Exception\InvalidArgumentException
161+
* @throws \Imagine\Exception\NotFoundException
160162
*/
161163
protected function checkLocalFile()
162164
{
163165
if (!is_file($this->path)) {
164-
throw new InvalidArgumentException(sprintf('File %s does not exist.', $this->path));
166+
throw new NotFoundException(sprintf('File %s does not exist.', $this->path));
165167
}
166168
if (!is_readable($this->path)) {
167169
throw new InvalidArgumentException(sprintf('File %s is not readable.', $this->path));
@@ -212,6 +214,7 @@ protected function isCurlSupported()
212214
* Read a remote file using the cURL extension.
213215
*
214216
* @throws \Imagine\Exception\InvalidArgumentException
217+
* @throws \Imagine\Exception\NotFoundException
215218
*
216219
* @return string
217220
*/
@@ -239,7 +242,7 @@ protected function readRemoteFileWithCurl()
239242
$responseInfo = curl_getinfo($curl);
240243
curl_close($curl);
241244
if ($responseInfo['http_code'] == 404) {
242-
throw new InvalidArgumentException(sprintf('File %s does not exist.', $this->path));
245+
throw new NotFoundException(sprintf('File %s does not exist.', $this->path));
243246
}
244247
if ($responseInfo['http_code'] < 200 || $responseInfo['http_code'] >= 300) {
245248
throw new InvalidArgumentException(sprintf('Failed to download "%s": %s', $this->path, $responseInfo['http_code']));

tests/tests/Image/AbstractImagineTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function testShouldFailOnUnknownImage()
145145
{
146146
$invalidResource = __DIR__ . '/path/that/does/not/exist';
147147

148-
$this->isGoingToThrowException('Imagine\Exception\InvalidArgumentException', sprintf('File %s does not exist.', $invalidResource));
148+
$this->isGoingToThrowException('Imagine\Exception\NotFoundException', sprintf('File %s does not exist.', $invalidResource));
149149
$this->getImagine()->open($invalidResource);
150150
}
151151

tests/tests/Image/Metadata/MetadataReaderTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testReadFromHttpFile()
5050

5151
public function testReadFromInvalidFileThrowsAnException()
5252
{
53-
$this->isGoingToThrowException('Imagine\Exception\InvalidArgumentException', 'File /path/to/no/file does not exist.');
53+
$this->isGoingToThrowException('Imagine\Exception\NotFoundException', 'File /path/to/no/file does not exist.');
5454
$this->getReader()->readFile('/path/to/no/file');
5555
}
5656

0 commit comments

Comments
 (0)