Home > Software design >  500 errors are not caught in prod environment
500 errors are not caught in prod environment

Time:02-11

I have a strange problem in a symfony 5.3.10 app and i can't wrap my head around it.

If I trigger a 500 error, like so:

/**
 * @Route("/", name="pvr_index", methods={"GET"})
 */
public function index(): Response
{
    echo $a;

Where $a is not defined, in dev environment I get a nice error page.

I'd expect that in prod I would get a not so nice error page that just tells me that i have a 500 error. And this would happen correctly until some time ago, when something changed, but I don't know what.

Now instead I get no error, not even in the symfony server console, and the script execution would keep on going like nothing happened.

What I have tried:

Adding the supposed exception inside a try/catch:

try {
        echo $a;
    }
    catch (\Exception $e)
    {
        dump($e);
        die();
    }

Nothing happens, the catch isn't triggered.


Creating an exception listener:

In services.yaml i have:

exception_listener:
    class: App\Event\ExceptionListener
    tags:
        - { name: kernel.event_listener, event: kernel.exception }

And in ExceptionListener.php i have:

class ExceptionListener
{
    public function __construct(Environment $engine) {
        $this->engine = $engine;
    }
    public function onKernelException(ExceptionEvent $event)
    {
        dd($event->getThrowable()->getMessage());
    }
}

Still nothing happens, the listener isn't triggered.

Everything works as expected in dev environment, the problem presents itself only in prod.

Looking at a phpinfo(); page I see that the error reporting is enabled, so I think that the problem lies in the symfony configuration.

I'm using php 7.4 and have no real access to the php.ini file

CodePudding user response:

This is not a Symfony error (nor a PHP error for that matter).

On PHP 7.4, trying to output an undefined variable would only raise a notice, not a fatal error (code 500). Notices are silently swallowed up by the engine unless you configure your server to log them or output them, where you'd get something like:

Notice: Undefined variable: a in file xxx.php line Y

A try/catch won't do anything for you, since no exception is raised.

Even on PHP 8 or 8.1, this will only raise a warning like:

Warning: Undefined variable $a

You can see the result under version < 8 and >= 8 here.

  • Related