How do I forward errors generated in the Bootstrap to the ErrorController?

喜你入骨 提交于 2019-12-07 07:01:26

If an Exception occurs in the Bootstrap then it cannot be passed to the ErrorController because your application has not been set up yet so it has no idea about the ErrorController yet.

You'll have to use something else to handle it.

Having an application which is highly likely to throw an uncaught Exception in the Bootstrap is surely not a good thing. You should catch the Exceptions that occur in the Bootstrap and handle them accordingly.

But if you connect to the database JIT (Just In Time), there should be no problem. You can actually move the database connection after bootstrap using Front Controller plugins. The DB connection setup can be at _dispatchLoopStartup() if you don't need it to setup other things (like routes, some pre-dispatch translations, etc).

//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);
}

This way every exception thrown during the db connection will be fired after $front->dispatch();

this is from my index.php, may be it'll be useful:

//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';
}

note: Zend_Registry::isRegistered('Zend_Log') Zend_Log instance registered with registry in my extended application resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!