Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds NotFoundException when file does not exit #853

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading