2010-08-22 19 views
5

Tôi đã tạo một vài người giúp đỡ để nắm bắt bất kỳ lỗi PPH nào và sau đó gửi một bài đăng curl để tạo một vé với phần mềm theo dõi lỗi của tôi.set_error_handler trong Codeigniter?

Nhưng tôi không thể làm cho nó hoạt động với Codeigniter. Đây là mã của tôi:


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

Đó là không phải tất cả đánh bóng, nhưng thats im mã sử dụng. (vài bit để dọn dẹp & xóa) Nó nằm trong một tệp trợ giúp đang được tự động nạp mã.

Bất kỳ ý tưởng nào?

Cảm ơn.

+1

Bạn có thể đăng thêm thông tin về vị trí chính xác không chính xác trong đơn đăng ký mã của bạn không? Một số mã từ bộ điều khiển/mô hình của bạn nơi bạn đang gọi trình trợ giúp lỗi của mình có thể hữu ích. – Shivaas

Trả lời

5

Bạn có thể thực hiện một vài điều nhanh chóng để tìm ra sự cố.

  1. Sử dụng mã ở trên trong tập lệnh php độc lập và tạo ra lỗi cố ý. Nếu mã hoạt động, sau đó cố gắng tìm cơ chế xử lý lỗi cụ thể codeigniter.

  2. Thay thế nội dung của hàm phpErrorHandler bằng một cái gì đó đơn giản, như in một cái gì đó trong nhật ký và xem liệu chính hàm đó đang được thực thi do lỗi. Nếu có, thì bạn chỉ cần sửa mã cho trình xử lý lỗi.

  3. Để xử lý lỗi cụ thể cho người viết mã, bạn có thể ghi đè lớp thư viện 'Ngoại lệ' bằng cách tạo lớp My_Exception trong thư mục ứng dụng/thư viện của bạn. Sao chép chữ ký chức năng thư viện ban đầu vào nó và đưa vào mã của bạn. Nó chắc chắn sẽ hoạt động.