Skip to content

[TwigBundle] ExceptionListenerPass does not seem to call removeDefinition with the right argument #38482

@adaniloff

Description

@adaniloff

Symfony version(s) affected: 4.4.15

Description
Since the 4.4.15 upgrade, we've got an error which is thrown by fos_rest:

[2020-10-08 12:22:23] php.CRITICAL: Argument 1 passed to FOS\RestBundle\Controller\ExceptionController::getStatusCode() must be an instance of Exception, instance of Symfony\Component\ErrorHandler\Exception\FlattenException given, called in /srv/api/vendor/friendsofsymfony/rest-bundle/Controller/ExceptionController.php on line 68 {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Argument 1 passed to FOS\RestBundle\Controller\ExceptionController::getStatusCode() must be an instance of Exception, instance of Symfony\Component\ErrorHandler\Exception\FlattenException given, called in /srv/api/vendor/friendsofsymfony/rest-bundle/Controller/ExceptionController.php on line 68 at /srv/api/vendor/friendsofsymfony/rest-bundle/Controller/ExceptionController.php:102)"} []

Prior to 4.4.15, we got not issue at all.

The real issue here is that we use a REST API, which usually throw errors in JSON. After the upgrade:

  • not only the exception is returned in HTML format
  • but even in dev env I do not have access neither to the stack trace nor the profiler !

It took some time to me but I finally figured out what caused the issue. However I'm not sure if my suggestion below is valid, so I'm creating this issue to see if I'm right - or not.

How to reproduce

  • not sure this can be reproduce easily and I've not much time to create a full test process right now. I'll do if this become necessary for you to investigate

Possible Solution
I'm just guessing that the issue here is having mixed the definition names... however I might miss something...
Please see my comments below as a fix suggestion (which solve the issue in my case obviously):

<?php
# In class ExceptionListenerPass implements CompilerPassInterface

    public function process(ContainerBuilder $container)
    {
        // ... skipping code

        if ($container->hasParameter('templating.engines')) {
            $engines = $container->getParameter('templating.engines');
            if (\in_array('twig', $engines, true)) {
                $container->removeDefinition('exception_listener'); // TO FIX: line 1 -> switch this line with line 2

                return;
            }
        }

        $container->removeDefinition('twig.exception_listener'); // TO FIX: line 2 -> switch this line with line 1
    }

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions