Skip to content

Commit fb777bf

Browse files
committed
Merge pull request #579 from trsteel88/error-logging
Update how missing filters are logged
2 parents e58ba83 + 4dac0f6 commit fb777bf

File tree

7 files changed

+61
-4
lines changed

7 files changed

+61
-4
lines changed

Controller/ImagineController.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
namespace Liip\ImagineBundle\Controller;
44

55
use Imagine\Exception\RuntimeException;
6+
use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
67
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
78
use Liip\ImagineBundle\Imagine\Data\DataManager;
89
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
910
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
1011
use Liip\ImagineBundle\Imagine\Cache\SignerInterface;
12+
use Psr\Log\LoggerInterface;
1113
use Symfony\Component\HttpFoundation\RedirectResponse;
1214
use Symfony\Component\HttpFoundation\Request;
1315
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -35,6 +37,11 @@ class ImagineController
3537
*/
3638
protected $signer;
3739

40+
/**
41+
* @var LoggerInterface
42+
*/
43+
protected $logger;
44+
3845
/**
3946
* @param DataManager $dataManager
4047
* @param FilterManager $filterManager
@@ -45,12 +52,14 @@ public function __construct(
4552
DataManager $dataManager,
4653
FilterManager $filterManager,
4754
CacheManager $cacheManager,
48-
SignerInterface $signer
55+
SignerInterface $signer,
56+
LoggerInterface $logger = null
4957
) {
5058
$this->dataManager = $dataManager;
5159
$this->filterManager = $filterManager;
5260
$this->cacheManager = $cacheManager;
5361
$this->signer = $signer;
62+
$this->logger = $logger;
5463
}
5564

5665
/**
@@ -87,6 +96,14 @@ public function filterAction(Request $request, $path, $filter)
8796
}
8897

8998
return new RedirectResponse($this->cacheManager->resolve($path, $filter), 301);
99+
} catch (NonExistingFilterException $e) {
100+
$message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $path, $e->getMessage());
101+
102+
if (null !== $this->logger) {
103+
$this->logger->debug($message);
104+
}
105+
106+
throw new NotFoundHttpException($message, $e);
90107
} catch (RuntimeException $e) {
91108
throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $path, $filter, $e->getMessage()), 0, $e);
92109
}
@@ -140,6 +157,14 @@ public function filterRuntimeAction(Request $request, $hash, $path, $filter)
140157
);
141158

142159
return new RedirectResponse($this->cacheManager->resolve($rcPath, $filter), 301);
160+
} catch (NonExistingFilterException $e) {
161+
$message = sprintf('Could not locate filter "%s" for path "%s". Message was "%s"', $filter, $hash.'/'.$path, $e->getMessage());
162+
163+
if (null !== $this->logger) {
164+
$this->logger->debug($message);
165+
}
166+
167+
throw new NotFoundHttpException($message, $e);
143168
} catch (RuntimeException $e) {
144169
throw new \RuntimeException(sprintf('Unable to create image for path "%s" and filter "%s". Message was "%s"', $hash.'/'.$path, $filter, $e->getMessage()), 0, $e);
145170
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Liip\ImagineBundle\Exception\Imagine\Filter;
4+
5+
use Liip\ImagineBundle\Exception\ExceptionInterface;
6+
7+
class NonExistingFilterException extends \RuntimeException implements ExceptionInterface
8+
{
9+
10+
}

Imagine/Filter/FilterConfiguration.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Liip\ImagineBundle\Imagine\Filter;
44

5+
use Liip\ImagineBundle\Exception\Imagine\Filter\NonExistingFilterException;
6+
57
class FilterConfiguration
68
{
79
/**
@@ -24,12 +26,12 @@ public function __construct(array $filters = array())
2426
*
2527
* @return array
2628
*
27-
* @throws \RuntimeException
29+
* @throws NonExistingFilterException
2830
*/
2931
public function get($filter)
3032
{
3133
if (false == array_key_exists($filter, $this->filters)) {
32-
throw new \RuntimeException(sprintf('Could not find configuration for a filter: %s', $filter));
34+
throw new NonExistingFilterException(sprintf('Could not find configuration for a filter: %s', $filter));
3335
}
3436

3537
return $this->filters[$filter];

Resources/config/imagine.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
<argument type="service" id="liip_imagine.filter.manager" />
110110
<argument type="service" id="liip_imagine.cache.manager" />
111111
<argument type="service" id="liip_imagine.cache.signer" />
112+
<argument type="service" id="logger" on-invalid="ignore" />
112113
</service>
113114

114115
<service id="liip_imagine.meta_data.reader" class="%liip_imagine.meta_data.reader.class%" public="false" />

Tests/Controller/ImagineControllerTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public function testCouldBeConstructedWithExpectedServices()
1818
$this->createDataManagerMock(),
1919
$this->createFilterManagerMock(),
2020
$this->createCacheManagerMock(),
21-
$this->createSignerMock()
21+
$this->createSignerMock(),
22+
$this->createLoggerMock()
2223
);
2324
}
2425

@@ -53,4 +54,12 @@ protected function createSignerMock()
5354
{
5455
return $this->getMock('Liip\ImagineBundle\Imagine\Cache\Signer', array(), array(), '', false);
5556
}
57+
58+
/**
59+
* @return \PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface
60+
*/
61+
protected function createLoggerMock()
62+
{
63+
return $this->getMock('Psr\Log\LoggerInterface');
64+
}
5665
}

Tests/DependencyInjection/LiipImagineExtensionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Liip\ImagineBundle\Tests\AbstractTest;
88
use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension;
99
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
use Symfony\Component\DependencyInjection\ContainerInterface;
1011
use Symfony\Component\DependencyInjection\Reference;
1112
use Symfony\Component\Yaml\Parser;
1213

@@ -45,6 +46,7 @@ public function testLoadWithDefaults()
4546
new Reference('liip_imagine.filter.manager'),
4647
new Reference('liip_imagine.cache.manager'),
4748
new Reference('liip_imagine.cache.signer'),
49+
new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
4850
)
4951
);
5052
}

Tests/Functional/Controller/ImagineControllerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ public function testShouldThrowNotFoundHttpExceptionIfFileNotExists()
102102
$this->client->request('GET', '/media/cache/resolve/thumbnail_web_path/images/shrodinger_cats_which_not_exist.jpeg');
103103
}
104104

105+
/**
106+
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
107+
*/
108+
public function testInvalidFilterShouldThrowNotFoundHttpException()
109+
{
110+
$this->client->request('GET', '/media/cache/resolve/invalid-filter/images/cats.jpeg');
111+
}
112+
105113
public function testShouldResolveWithCustomFiltersPopulatingCacheFirst()
106114
{
107115
/** @var Signer $signer */

0 commit comments

Comments
 (0)