简体   繁体   中英

php custom error handler

Hello everybody a probably easy question.

I need a custom error handler to report the notice back from a getJson call, and not violate any rule about json format of response.

So I thought of collecting all notice in a session variable and then add in a json_encode of the response

In my error handler the switch does not catch any option

<?php
session_start();

function myErrorHandler($errno, $errstr, $errfile, $errline) {
if (!(error_reporting() & $errno)) {
    // This error code is not included in error_reporting
    return;
}

switch ($errno) {
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";
     $_SESSION['Errors']['Errors'][]=$error;
//exit(1);
    break;

case E_USER_WARNING:
    $_SESSION['Errors']['Warning'][] = "<b>My WARNING</b> [$errno] $errstr<br />";
    break;

case 8: // notice
    if(isset($_REQUEST['ajax']) || isset($_REQUEST['ajaxAccess']) )         {
        $_SESSION['Errors']['Notice'][]="<b>My NOTICE</b> [$errno] $errstr $errfile $errline<br />";
        //json_encode($_SESSION);
        }

 //        else $error.= "<b>My NOTICE</b> [$errno] $errstr $errfile $errline<br />\n";
    break;

default:
 //        $error.= "Unknown error type: [$errno] $errstr<br />\n";
    break;
}

/* Don't execute PHP internal error handler */
return true;
 }

 $old_error_handler = set_error_handler("myErrorHandler");

The problem $errno is a number and does not match any of the option below

Do I probably have to changing something in configuration to have a string like that and make it works?

Thanks!

Your code will only handle errors triggered by you - errors caused by a call to trigger error() . In order to catch errors thrown by regular PHP functions and actions, you need to handle those constants as well, most notably E_WARNING and E_NOTICE (you cannot handle E_ERROR ).

You can easily modify your switch to match those as well:

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

  if (!(error_reporting() & $errno)) {
    // This error code is not included in error_reporting
    return;
  }

  switch ($errno) {
    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";
      $_SESSION['Errors']['Errors'][] = $error;
      // exit(1);
      break;
    case E_WARNING:
    case E_USER_WARNING:
      $_SESSION['Errors']['Warning'][] = "<b>My WARNING</b> [$errno] $errstr<br />";
      break;
    case E_NOTICE:
    case E_USER_NOTICE: // notice
      if(isset($_REQUEST['ajax']) || isset($_REQUEST['ajaxAccess']) )         {
        $_SESSION['Errors']['Notice'][] = "<b>My NOTICE</b> [$errno] $errstr $errfile $errline<br />";
        // json_encode($_SESSION);
      }
      // else $error.= "<b>My NOTICE</b> [$errno] $errstr $errfile $errline<br />\n";
      break;
    default:
      // $error.= "Unknown error type: [$errno] $errstr<br />\n";
      break;
  }

  /* Don't execute PHP internal error handler */
  return true;

}

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