Codeigniter中的set_error_handler?

时间:2010-08-22 23:52:10

标签: php oop codeigniter error-handling

我已经创建了一些助手来捕获任何PPH错误,然后发送一个curl帖子,它将使用我的bug跟踪软件创建一张票。

但我无法与Codeigniter合作。这是我的代码:


<? function phpErrorHandler($errno, $errstr, $errfile, $errline, $errcontext){

    //If is a notice do nothing.
    if($errno == E_NOTICE | $errno == E_USER_NOTICE): null; else:

    //Error types
    $errortype = array(
      E_ERROR           => 'Error',
      E_WARNING         => 'Warning',
      E_PARSE           => 'Parsing Error',
      E_NOTICE          => 'Notice',
      E_CORE_ERROR      => 'Core Error',
      E_CORE_WARNING    => 'Core Warning',
      E_COMPILE_ERROR   => 'Compile Error',
      E_COMPILE_WARNING => 'Compile Warning',
      E_USER_ERROR      => 'User Error',
      E_USER_WARNING    => 'User Warning',
      E_USER_NOTICE     => 'User Notice',
      E_STRICT          => "Runtime Notice");

    //Just get the filename for the title.
    $filenameGoBoom = explode('/',print_r( $errfile, true));
    $filename = end($filenameGoBoom);

    //MySQL
    if ($errstr == "(SQL)"):
        $errstr = "SQL Error [$errno] " . SQLMESSAGE . "<br />\n";
        $errstr .= "Query : " . SQLQUERY . "<br />\n";
        $errstr .=  "On line " . SQLERRORLINE . " in file " . SQLERRORFILE . " ";
    endif;

    //Write the report and set out the data
    $prefix = ($errortype[$errno]) ? $errortype[$errno] : "Unknown";
    $title = ($errortype[$errno]) ? $filename ." on line ". print_r( $errline, true) : "Unknown error type: [$errfile:$errline] [$errno] $errstr";
    $error = "\n\n---Data---\n". print_r( $errstr, true).
    "\n\n---File---\n". print_r( $errfile, true). "on line ". print_r( $errline, true)." @".date('Y-m-d H:i:s').
    "\n\n---Context---\n".print_r( $errcontext, true).
    "\n\n". print_r( debug_backtrace(), true);

        //Send! Show the error and stop executing the code
        sendError($prefix, $title , $error);

        echo "Whoops, something went wrong, it could of been something you did, or something we've done or didn't expect. Please try again, this error has been reported to us.".$errline.$errstr;

        die();

    endif;
}

function mysqlErrorHandler(){

    //Setup variables.
    $prefix = 'MySQL';
    $title = 'Error - '.mysql_errno();
    $error = 'MySQL Error '.mysql_errno().':'.mysql_error();

    //Send! 
    sendError($prefix,$title,$description);

    //Kill the script
    die();

}

function fatalErrorHandler(){

    $isError = false;
    if ($error = error_get_last()):
        switch($error['type']){
            case E_ERROR:
            case E_CORE_ERROR:
            case E_COMPILE_ERROR:
            case E_USER_ERROR:
            $isError = true;
            break;
            }
        endif;

    if ($isError):
        $prefix = 'Fatal';
        $title = 'Check this out';
        $error = $error['message'];

        sendError($prefix, $title , $error);

        echo "Whoops, something went wrong, it could of been something you did, or something we've done or didn't expect. Please try again, this error has been reported to us.";

    endif; 
}

set_error_handler('phpErrorHandler');
register_shutdown_function('fatalErrorHandler');
?>

它并非全部抛光,但这就是我正在使用的代码。 (清理和删除的几个位)它位于一个帮助文件中,由codeigniter自动加载。

有什么想法吗?

感谢。

1 个答案:

答案 0 :(得分:5)

你可以快速做一些事情来解决问题。

  1. 在独立的php脚本中使用上述代码并创建故意错误。如果代码有效,则尝试查找codeigniter特定的错误处理机制。

  2. 用简单的东西替换phpErrorHandler函数的内容,比如在日志中打印一些东西,看看函数本身是否正在执行时出错。如果是,那么您只需要修复错误处理程序的代码。

  3. 对于codeigniter特定的错误处理,您可以通过在application / libraries文件夹中创建My_Exception类来覆盖其“Exception”库类。将原始库函数签名复制到其中并输入您的代码。它肯定会奏效。