Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question

1 Answer

up vote 1 down vote accepted

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;

}
share|improve this answer
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 ;) – giuseppe May 24 '12 at 16:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.