symfony: [ErrorHandler] : Throw exception on notice in debug mode
Symfony version(s) affected: 5.0.2 (and prior i guess)
Description
A notice (for example : “Notice: Undefined variable: index”), throws an exception in debug mode in dev env.
How to reproduce
Create a basic Command file TestCommand.php
Set env variables like this :
- dev => true
- debug => true
protected function configure() {
$this->setName('app:test');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output) {
echo $index;
echo 'toto';
return 0;
}
Launch the following command from terminal :
php bin/console app:test
It will throw an exception in console and not show ‘toto’.
Possible Solution
In debug mode, if it’s not an error or higher (critical, fatal adn so on), it doesn’t have to throw an exception, but only display a message and continue the execution of the command.
Additional context
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 17 (14 by maintainers)
Not accepting a single deprecation notice during dev is a best practice that is enforced since the first days of Symfony. It encourages writing better code. That’s desired.
You’re link helped me to find my solution. I only add the parameter debug.error_handler.throw_at to 0 in order to have the same behavior between all my envs.
Thanks @jameslmoser
I was able to resolve this by changing this to false: https://symfony.com/doc/current/reference/configuration/framework.html#throw
Hope that helps.
I understand it’s good practice to eliminate any notice level errors, but when this notice occurs in third-party library, doctrine for instance, having no control to stop this exception is very annoying.
I’m sorry but this is not how we’re going to make progress in this discussion. The majority here will think that an exception should be thrown when a PHP notice happen, I can bet on it.
By the way, this has been the Symfony philosophy since years. Being more strict during development to enable the developer to code as carefully as possible while not throwing on everything in production to not influence the user’s experience too badly (see the behaviour for unknown variables in Twig templates for example).
I’m not sure a notice should throw and exception - the definition of E_NOTICE in the php docs is:
If a notice threw an exception it would potentially break otherwise working/correct code. I’ve seen notices generated from bad config in php.ini when running good code.
If this is required, perhaps a flag to enable exceptions on notice would be better.