2

Just like in debug mode, I'd like Symfony to catch any notice or warning in my prod environment, and convert it to an exception.

Can I do this without enabling the whole debug mode?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • 1
    Not sure if your question is clear enough. Symfony DOES catch all notices and exceptions, and with debug enabled they are shown on screen and stored in the profiler. What is it exactly that you want to happen? – Francesco Abeni Apr 29 '21 at 06:54
  • Sorry if my question is unclear. I mean that in production, warnings are not caught and, if I'm not mistaken, just logged. I would like a 500 error page as well, like in dev. – BenMorel Apr 30 '21 at 07:20
  • It happens to me that in a `try { $this->something() } catch( \Exception $e ) { ... }` if in `something()` I do a type error or I do a `1/0;` in `dev` it gets properly catched and controlled, but in `prod` I get a 500 server error. I just want to catch errors as exceptions as in `dev` but in `prod` but without doing any other change (no debug, no env in prod, etc. as Francesco Abeni suggests; nor creating an event listener). It must be a matter of "configuration", no? How can this be achieved? – Xavi Montero Mar 28 '22 at 23:03

3 Answers3

0

Symfony already catches exceptions, the difference between DEV and PROD (in their default configuration) is that DEV shows you a 500 error page with stack trace and all details, while PROD shows you a "silent" 500 error page.

That is intended: You should not expose those details in production.

If your production instance is safe (e.g. used internally) you may choose one the following two options.

  1. Enable debug mode: in your .env or .env.local file on the server, set:

    APP_DEBUG=true

This is probably the closest answer to your original question.

  1. Use dev Symfony mode: in your .env or .env.local file on the server, set:

    APP_ENV=dev

This also changes other behaviours (e.g. more detailed logs, include other configuration files). See https://symfony.com/doc/current/configuration.html#configuration-environments for additional details.

Blackbam
  • 17,496
  • 26
  • 97
  • 150
Francesco Abeni
  • 4,190
  • 1
  • 19
  • 30
  • Sorry, I fixed a gross typo in my question: I meant any notice or **warning**. AFAICS, warnings are converted to exceptions in dev mode, but silently discarded (or logged) in production? i.e. they allow the code to continue being executed, when I'd like a warning to trigger a 500 as well. – BenMorel Apr 30 '21 at 20:15
0

Indeed you can catch PHP warnings and convert them to exceptions in production (not sure if this makes sense for notices but you can also do it for notices).

Basically the question is old and was already discussed in detail: Can I try/catch a warning?

Just setup your own error handler (https://www.php.net/manual/en/function.set-error-handler.php) with a custom event listener on kernel.request (which is always called very early) (https://symfony.com/doc/current/reference/events.html#kernel-request):

namespace App\EventListener;

use Symfony\Component\HttpKernel\Event\RequestEvent;

class ProdWarningToExceptionListener {

    private $environment;

    public function __construct(KernelInterface $kernel)
    {
        $this->environment = $kernel->getEnvironment();
    }

    public function onKernelRequest(RequestEvent $event)
    {
        if ($this->environment === "prod") {
            set_error_handler(function($errno, $errstr, $errfile, $errline) {
                // $errno contains the error level, do with it whatever you want
                throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
            });
        }
    }

}

Note that this is pseudo-code, you might have to adjust it a little bit. But basically it should solve your problem.

Blackbam
  • 17,496
  • 26
  • 97
  • 150
0

https://symfony.com/doc/current/reference/configuration/framework.html#php-errors

config/packages/framework.yaml

framework:
    ....
    php_errors:
        throw: true
Alexander
  • 321
  • 1
  • 2
  • 6