views:

40

answers:

1

Hey, i've created a few helpers to catch any php errors and then send a curl post which will create a ticket with my bug tracking software.

But i can't get it to work with code igniter.

Here is my code.


<? 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');
?>

It's not all polished, but thats the code im using. (few bits to clean up & remove) It's in a helper file which is being autoloaded by codeigniter.

Any ideas?

Thanks.

A: 

You can do a few things quickly to figure out the problem.

  1. Use the above code in a standalone php script and create an intentional error. If the code works, then try to find codeigniter specific error handling mechanism.

  2. Replace the contents of the phpErrorHandler function with something simple, like printing something in the log and see if the function itself is being executed on error. If yes, then you only need to fix the code for the error handler.

  3. For codeigniter specific error handling, you can override its 'Exception' library class by creating My_Exception class in your application/libraries folder. Copy original library function signatures into it and put in your code. It will surely work.

Samnan