Skip to content

Commit 32ac4ae

Browse files
committed
Catch error with entrypointlookups in sub-request by Twig exceptions
Twig_Error_Runtime leads to sub-requests. In EntrypointLookup, the list of previously returned entries is not reset on these requests. Fixes symfony/demo#910
1 parent a3382c8 commit 32ac4ae

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony WebpackEncoreBundle package.
5+
* (c) Fabien Potencier <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfony\WebpackEncoreBundle\EventListener;
11+
12+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
13+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
14+
use Twig_Error_Runtime;
15+
16+
class ExceptionListener
17+
{
18+
private $entrypointLookup;
19+
20+
public function __construct(EntrypointLookup $entrypointLookup)
21+
{
22+
$this->entrypointLookup = $entrypointLookup;
23+
}
24+
25+
public function onKernelException(GetResponseForExceptionEvent $event)
26+
{
27+
$exception = $event->getException();
28+
29+
// Reset the entrypointLookup list of previously returned entries, as Twig_Error_Runtime will initialise a sub-request
30+
if ($exception instanceof Twig_Error_Runtime) {
31+
$this->entrypointLookup->reset();
32+
}
33+
}
34+
}

src/Resources/config/services.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
<argument /> <!-- entrypoints.json path -->
1212
</service>
1313

14+
<service id="webpack_encore.exceptionlistener" class="Symfony\WebpackEncoreBundle\EventListener\ExceptionListener">
15+
<argument type="service" id="webpack_encore.entrypoint_lookup" />
16+
<tag name="kernel.event_listener" event="kernel.request" />
17+
</service>
18+
1419
<service id="webpack_encore.tag_renderer" class="Symfony\WebpackEncoreBundle\Asset\TagRenderer">
1520
<argument type="service" id="webpack_encore.entrypoint_lookup" />
1621
<argument type="service" id="assets.packages" />

0 commit comments

Comments
 (0)