2013-08-19 30 views
1

我目前正在開發一個基於Zend 1.11的項目,我需要捕獲與數據庫相關的異常,並在出現通知時顯示通知。不用說,我完全新的Zend框架...如何在Zend的(1.11)ErrorController中捕獲數據庫異常?

通過我在ErrorController中定義的默認操作看來看,我沒有對如何實現這一頭緒:

class ErrorController extends Zend_Controller_Action 
{ 
    private $logPriority_; 

    public function errorAction() 
    { 
     $errors = $this->_getParam('error_handler'); 

     if (!$errors || !$errors instanceof ArrayObject) 
      $this->_forward('notfound','error'); 

     switch ($errors->type) { 
      case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE: 
      case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER: 
      case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: 
       // 404 error -- controller or action not found 
       $this->_forward('notfound','error'); 
       break; 
      default: 
       // application error 
       break; 
     } 

     $this->getResponse()->setHttpResponseCode(500); 
     $this->view->message = '500 Internal Server Error'; 
     $this->logErrors(Zend_Log::CRIT); 
    } 
    // ... 

我應該在哪裏以及如何處理這個問題?

+0

錯誤控制的目的是捕捉異常,並顯示在默認情況下,用戶友好的錯誤消息 - 當你說你想要的應用「顯示通知',這與現在發生的事情有什麼不同? –

+0

那麼,我特別需要的是(在錯誤控制器中)確定拋出的異常與訪問數據庫有關,在這種情況下,我應該顯示一條消息。 –

回答

2

您可以檢查是否Zend_Db_Exception拋出:

if($errors->exception and $errors->exception instanceof Zend_Db_Exception) { 
    // do something 
} 
+0

我做了一個修改,顯然,這是一個很好的解決方案:'if($ errors-> exception和$ errors-> exception instanceof Model_Database_DatabaseException){}'' –