2010-08-22 16 views
5

He creado algunos ayudantes para detectar cualquier error de PPH y luego envío una publicación curl que creará un ticket con mi software de seguimiento de errores.set_error_handler en Codeigniter?

Pero no puedo hacer que funcione con Codeigniter. Aquí está mi código:


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

No todo pulido, pero eso es el código im utilizando. (algunos bits para limpiar & eliminar) Está en un archivo auxiliar que está siendo cargado automáticamente por codeigniter.

¿Alguna idea?

Gracias.

+1

¿Puede publicar más información sobre dónde está exactamente fallando en su aplicación codeigniter? Algún código de su controlador/modelo al que está llamando su ayudante de error podría ayudar. – Shivaas

Respuesta

5

Puede hacer algunas cosas rápidamente para resolver el problema.

  1. Utilice el código anterior en una secuencia de comandos php independiente y cree un error intencionado. Si el código funciona, intente encontrar el mecanismo específico de manejo de errores codeigniter.

  2. Reemplace el contenido de la función phpErrorHandler con algo simple, como imprimir algo en el registro y ver si la función se está ejecutando por error. En caso afirmativo, solo necesita corregir el código del controlador de errores.

  3. Para el manejo de errores específicos de codeigniter, puede anular su clase de biblioteca 'Excepción' creando la clase My_Exception en su carpeta de aplicaciones/bibliotecas. Copie las firmas originales de la función de la biblioteca y ponga su código. Seguramente funcionará.