Skip to content

Catch error with entrypointlookups in sub-requests #21

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

Closed
wants to merge 4 commits into from
Closed
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
34 changes: 34 additions & 0 deletions src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony WebpackEncoreBundle package.
* (c) Fabien Potencier <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\WebpackEncoreBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookup;
use Twig_Error_Runtime;

class ExceptionListener
{
private $entrypointLookup;

public function __construct(EntrypointLookup $entrypointLookup)
{
$this->entrypointLookup = $entrypointLookup;
}

public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();

// Reset the entrypointLookup list of previously returned entries, as Twig_Error_Runtime will initialise a sub-request
if ($exception instanceof Twig_Error_Runtime) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this error specifically? All exceptions will result in a full page being rendered (by default). So, I think what you’re doing is trying to detect if the error came from Twig, which is the most likely cause of a situation where Encore assets have been rendered already... and THEN an exception happens. Am I right?

If so... I’m not sure it’s a good idea. Regardless of the error, if we try to render a template for an error page, that should probably use a fresh set of Encore assets.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m also trying to think if we should limit this to the master request... does the default exception behavior (rendering an error template) happen only on the master request? That seems likely - I just haven’t checked the code yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've thought more about this. I think we should:

  1. Always reset on exception - not just if there was a Twig error. There is going to be some edge cases (people successfully rendering in Twig, then a listener throws an exception after).

  2. We should reset on all requests, not just the master request.

There may still be edge cases - but let's do that and see what happens. Fortunately, this is in "exception" land - so it's already not a normal request.

$this->entrypointLookup->reset();
}
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<argument /> <!-- entrypoints.json path -->
</service>

<service id="webpack_encore.exceptionlistener" class="Symfony\WebpackEncoreBundle\EventListener\ExceptionListener">
<argument type="service" id="webpack_encore.entrypoint_lookup" />
<tag name="kernel.event_listener" event="kernel.exception" />
</service>

<service id="webpack_encore.tag_renderer" class="Symfony\WebpackEncoreBundle\Asset\TagRenderer">
<argument type="service" id="webpack_encore.entrypoint_lookup" />
<argument type="service" id="assets.packages" />
Expand Down