简体   繁体   中英

php custom error handler how to print error in production?

I have an interface calling some php function by ajax.

There is an uncatched error that I have only in production. Due I make estensive use of getJson data exchange , I needed to silent errors and warnings with a custom php error handler.

The problem is that anyway when code generates an error in production I wish to print it out to the user, to understand where the problem is. My code is :

function myErrorHandler($errno, $errstr, $errfile, $errline)
{

switch ($errno) {
case E_ERROR:
    case E_USER_ERROR:
    $error= "<b>My ERROR</b> [$errno] $errstr<br />\n";
    $error.= "  Fatal error on line $errline in file $errfile";
    $error.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
    $error.= "Aborting...<br />\n";
    if(isAjaxCalling()){
        $_SESSION['Errors']['Errors'][]=$error;
    }

    ini_set('display_errors',1);
    ini_set('error_reporting','E_ALL');
    echo $error;
    exit(1);
    break;
case E_WARNING:
case E_USER_WARNING:

....

}

You need to let know the ajax handler on the client side that the returned value is in fact an error message, best to do it with status header:

header("HTTP/1.0 500 Internal server error");

Then either globaly, or individualy per ajax request, set function that will handle the error. In jQuery, how to do it globaly: http://api.jquery.com/ajaxError/ or individualy set 'error' option of ajax request.

Seems it's now printed with this mod:

case E_ERROR:
case E_USER_ERROR:
$error= "<b>My ERROR</b> [$errno] $errstr<br />\n";
$error.= "  Fatal error on line $errline in file $errfile";
$error.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
$error.= "Aborting...<br />\n";
if(isAjaxCalling()){
    $_SESSION['Errors']['Errors'][]=$error;
}

exit( $error);
break;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM