2011-04-09 15 views
1

我使用的是Zend Tool爲我生成的默認ErrorController。我index.php文件做的第一件事是要註冊轉換錯誤和警告到異常錯誤處理程序:如何將Bootstrap中生成的錯誤轉發給ErrorController?

function handleError($errno, $errstr, $errfile, $errline, array $errcontext) { 
    // error was suppressed with the @-operator 
    if (0 === error_reporting()) { 
     return false; 
    } 

    throw new ErrorException($errstr, 0, $errno, $errfile, $errline); 
} 

set_error_handler('handleError'); 

我也有這個成立於index.php

$front = Zend_Controller_Front::getInstance(); 
$front->throwExceptions(false); 

的問題是,它不處理來自Bootstrap文件的異常。異常點燃,但ErrorController不接受。頁面的執行只會暫停,並且不會顯示任何視圖。

如何通過錯誤控制器路由每個異常和警告(當然,語法和致命錯誤除外)?

回答

1

如果Bootstrap中發生異常,那麼它不能傳遞到ErrorController,因爲您的應用程序尚未設置,所以它不知道ErrorController

你必須使用別的東西來處理它。

有一個很有可能在Bootstrap中拋出未捕獲異常的應用程序肯定不是一件好事。您應該捕獲Bootstrap中發生的異常並相應地處理它們。

+0

我同意。我懷疑Bootstrap中的任何操作都會失敗。我能想象的唯一事情就是連接到數據庫。在這種情況下,我想我只會顯示一個普通的錯誤頁面。 – 2011-04-09 20:33:31

2

但是,如果您連接到數據庫JIT(Just In Time),應該沒有問題。您可以使用Front Controller插件在引導後實際移動數據庫連接。如果您不需要它來設置其他內容(如路線,某些預調度翻譯等),則數據庫連接設置可以在_dispatchLoopStartup()

//in bootstrap 
$front->registerPlugin(new Awesome_Db_Plugin($zendConfigWithDbOptions)); 
// in plugin (options injected via constructor to private member 
public function dispatchLoopStartup() { 
    $db = Zend_Db::factory($this->_options); 
    Zend_Registry::set('db', $db); 
} 

這樣的數據庫連接時拋出的每一個異常會後$front->dispatch();

+0

這是一個好主意!謝謝 – 2011-04-10 20:49:01

2

這是從我的index.php被解僱,可能是這將是有用的:

//bootstrap, and run application 
try { 
    require_once 'Zend/Application.php'; 
    //create application and configure it. 
    $application = new Zend_Application(
     getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production', 
     array('config' => array(APPLICATION_PATH . DS . 'configs' . DS . 'application.ini')) 
    ); 
    //run application 
    $application->bootstrap()->run(); 
} catch (Exception $e) { 
    //fallback for uncatched exceptions 
    ob_clean(); 
    //ensure error will be logged and firephp backend, if enabled, will send log messages 
    if(is_object($application) 
     && $application->bootstrap('log') 
     && Zend_Registry::isRegistered('Zend_Log') 
    ) { 
     Zend_Registry::get('Zend_Log') 
      ->log($e,Zend_Log::CRIT); 
    } 
    $wildfire = Zend_Wildfire_Channel_HttpHeaders::getInstance(); 
    if(!($response = Zend_Controller_Front::getInstance()->getResponse())) { 
     $response = new Zend_Controller_Response_Http(); 
     $wildfire->setRequest(new Zend_Controller_Request_Http()); 
     $wildfire->setResponse($response); 
    } 
    if($response->canSendHeaders()) { 
     $response->clearHeaders(); 
     $response->setHttpResponseCode(500); 
     $wildfire->flush(); 
     $response->sendResponse(); 
    } 
    //put static html for error page here 
    echo 'Startup error occured. Try again later'; 
} 

Zend_Registry::isRegistered('Zend_Log') Zend_Log實例在我的擴展應用程序資源中的註冊表中註冊了