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?
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?
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.
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.
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.
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.
https://symfony.com/doc/current/reference/configuration/framework.html#php-errors
config/packages/framework.yaml
framework:
....
php_errors:
throw: true