问题
I'm having PHP + MySQL code base. If MySQL is stopped and during log-in it fails like this:
Fatal error: Uncaught exception 'FrameworkException' with message 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)' in /var/www/html/classes/class.connection.php:24
Stack trace: #0 /var/www/html/login.php(15): Connection::getInstance('localhost', 'user', '__password', 'DB') #1 /var/www/html/login.php(73): isDataValid('lakshmipathi.g@...', 'test') #2 {main} thrown in /var/www/html/classes/class.connection.php on line 24
How to avoid this stack trace? I want to avoid the username and password displayed like this in case of errors
回答1:
On a production server you should hide the error with the display_errors directive of your php.ini The error still be logged but not displayed to the end user. Turning off the error with error_reporting is not a good idea if you need to debug
Nevertheless you should handle the exception :
try {
//Here the code which can throw an exception
} catch(FrameworkException e) {
echo 'an exception occured';
}
回答2:
I'd recommend using @mysql_connect
(or @ + whatever function you call) to suppress any error messages. And manually spit out some error. Since you are using some kind of mysterious framework, you could simply catch the exception.
But, for live environments, you should disable any kind error reporting to the user anyway.
来源:https://stackoverflow.com/questions/5262245/how-to-avoid-strace-in-php