For any PHP project I think it’s mandatory to have a good way of dealing with errors, just because shit happens 😉
The idea of the next, is to have a basic knowledge about how to handle errors and errors reporting to the development team.
First, and something never should happen it’s to display errors to the end user (browser).
To prevent it:
define('ENVIRONMENT', 'prod');//set to "dev" for development environment
if (ENVIRONMENT=="prod"){
error_reporting(0);//comment for error reporting
ini_set('display_errors','off');//set ON to see errors
}
As you can see I’ve set a ENVIRONMENT variable. This is because in development I like to see error, and the error handling should work way different from production.
Once we have this we can move forward.
Second, and not less important is to have our own customized error handler. In this way you can say what should happen every time an error is thrown.
To do this we need to use the function from php set_error_handler.
To use this functionality we need to have our own function to handle errors, this is mine:
function myErrorHandler($errno, $errstr, $errfile, $errline){
switch ($errno) {
case E_USER_ERROR:
if ($errstr == "(SQL)"){ // handling an sql error
$error_body= "SQL Error [$errno] " . SQLMESSAGE . "
n
Query : " . SQLQUERY . "
n
On line " . SQLERRORLINE . " in file " . SQLERRORFILE . "
, PHP " . PHP_VERSION . " (" . PHP_OS . ")
n
Aborting...
n";
} else {
$error_body= "ERROR CRITICAL [$errno] $errstr
n
Fatal error on line $errline in file $errfile
, PHP " . PHP_VERSION . " (" . PHP_OS . ")
n
Aborting...
n";
}
if (ENVIRONMENT=="dev") echo $error_body;
elseif (ENVIRONMENT=="prod"){//send email
mail("HERE YOUR MAIL","ERROR $errno "."here your site name", $error_body,'From: Error Handler');
}
exit(1);
break;
case E_USER_WARNING:
if (ENVIRONMENT=="dev") echo "ERROR WARNING [$errno] $errstr
n";
break;
case E_USER_NOTICE:
if (ENVIRONMENT=="dev") echo "NOTICE [$errno] $errstr
n";
break;
default:
if (ENVIRONMENT=="dev") echo "Unknown error type: [$errno] $errstr
n";
break;
}
return true;
}
function mySqlError($ERROR, $QUERY, $PHPFILE, $LINE){//hadle mysql error
define("SQLQUERY", $QUERY);
define("SQLMESSAGE", $ERROR);
define("SQLERRORLINE", $LINE);
define("SQLERRORFILE", $PHPFILE);
trigger_error("(SQL)", E_USER_ERROR);
}
With this code we will receive an email to the specified address, or just an echo if you are in development.
To use it, simply after this code for example, we must add this:
set_error_handler('myErrorHandler');
Also and really important is to trace MySql errors, for this I used the function mySqlError, that throws an error exception to the error handler (myErrorHandler).
To allow it just your mysql_query funtion should be something like this example:
function app_query($query) {
$return_val=@ mysql_query($query) or mySqlError("(".mysql_errno().") ".mysql_error(), $query, $_SERVER['PHP_SELF'], __LINE__);
return $return_val;
}
As you can see with this simple steps we have almost full control over the errors in you web app 😉
Everything here you can find it in Open Classifieds implemented and works pretty good.