English 中文(简体)
php 自定义错误处理器
原标题:php custom error handler

大家好,大家好 问一个可能很简单的问题。

我需要一个自定义的错误处理程序 将通知从接通Json的电话中报告回来, 而不是违反任何关于 Json 回应格式的规则。

于是我想在会话变量中收集所有通知, 然后添加到响应的json_encode

在我的错误处理器中, 开关无法抓住任何选项

<?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 />
";
    $error.= "  Fatal error on line $errline in file $errfile";
    $error.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />
";
    $error.= "Aborting...<br />
";
     $_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 />
";
    break;

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

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

 $old_error_handler = set_error_handler("myErrorHandler");

问题$errno是一个数字,与以下任何选项不匹配

难道我必须改变配置 才能有这样一条绳子 才能让它正常工作吗?

谢谢!

最佳回答

您的代码将只处理由您引发的错误 - 调用 < code> trigger 错误 () 导致的错误。 为了捕捉由普通 PHP 函数和动作造成的错误, 您需要同时处理这些常数, 特别是 < code> E_ WARNING 和 < code> E_ NOTITE (您无法处理 < code> E_ ERROR ) 。

您可以很容易地修改您的 switch 来匹配这些 :

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 />
";
      $error.= "  Fatal error on line $errline in file $errfile";
      $error.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />
";
      $error.= "Aborting...<br />
";
      $_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 />
";
      break;
    default:
      // $error.= "Unknown error type: [$errno] $errstr<br />
";
      break;
  }

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

}
问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签