0

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!

1 Answer 1

1

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;

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. it means that with _USER they are triggered by me and without they are triggered by php I suppose . I try and get back ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.