
1.錯誤處理
發生錯誤后引發 handleError() 函數處理:public function handleError($code,$message,$file,$line) { if($code & error_reporting()) { // disable error capturing to avoid recursive errors restore_error_handler(); restore_exception_handler(); //restore_error_handler,restore_exception_handler兩個函數,如果沒有這兩個函數的調用,那么在后續的錯誤處理過程中,當再次產生異?;蚴清e誤時,又會調用Capplication:: handleError ,從而可能造成死循環,故Yii在此處臨時禁止了使用CApplication:: handleError 接管后續的錯誤和異常(使用php默認的錯誤處理機制),這就保證了不會因之產生循環調用。 $log="$message ($file:$line)/nStack trace:/n"; $trace=debug_backtrace(); // skip the first 3 stacks as they do not tell the error position if(count($trace)>3) { $trace=array_slice($trace,3); } foreach($trace as $i=>$t) { if(!isset($t['file'])) { $t['file']='unknown'; } if(!isset($t['line'])) { $t['line']=0; } if(!isset($t['function'])) { $t['function']='unknown'; } $log.="#$i {$t['file']}({$t['line']}): "; if(isset($t['object']) && is_object($t['object'])) { $log.=get_class($t['object']).'->'; } $log.="{$t['function']}()/n"; } if(isset($_SERVER['REQUEST_URI'])) { $log.='REQUEST_URI='.$_SERVER['REQUEST_URI']; } Yii::log($log,CLogger::LEVEL_ERROR,'php'); try { Yii::import('CErrorEvent',true); $event=new CErrorEvent($this,$code,$message,$file,$line); //此時將產生一個CErrorEvent(并包含$code,$message,$file,$line幾項關鍵參數), 傳遞給CErrorHandler組件進行處理。具體是交給CErrorHandler::handleError處理之 。這個流程主要是將錯誤相關信息進行整理,并以合適的方式進行顯示。 $this->onError($event); if(!$event->handled) { // try an error handler if(($handler=$this->getErrorHandler())!==null) { $handler->handle($event); } else { $this->displayError($code,$message,$file,$line); } } } catch(Exception $e) { $this->displayException($e); } try { $this->end(1); } catch(Exception $e) { // use the most PRimitive way to log error $msg = get_class($e).': '.$e->getMessage().' ('.$e->getFile().':'.$e->getLine().")/n"; $msg .= $e->getTraceAsString()."/n"; $msg .= "Previous error:/n"; $msg .= $log."/n"; $msg .= '$_SERVER='.var_export($_SERVER,true); error_log($msg); exit(1); } } }

2.異常處理

3.使用一個動作來處理錯誤

新聞熱點
疑難解答