doujianchao7446 2014-05-02 22:06
浏览 61

当引导抛出异常时,Zend Framework显示自定义错误

I am currently using Zend Framework 1. In bootstrap i will have

protected function _initLogger() {

  //this is where I create log to file

}

The issue is when the log file have write protected or have issue the error will display to browser with directory location etc.

I don't want to suppress the message but how can I custom this error so it will not show too much information

my application.ini

phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1

  • 写回答

1条回答 默认 最新

  • dongsonghen9931 2014-05-03 07:10
    关注

    Try this.

    Tell Zend Framework to not handle exceptions. Do this in your application.ini

    resources.frontController.throwExceptions = 1
    

    Do following in your Bootstrap class.

    Define a custom method to handle exceptions.

    public function __handleExceptions(Exception $e){
            //render a view with a simple error message for the user
            //and send an email with the error to admin
        }
    

    Override the _bootstrap() and run() methods of Zend_Application_Bootstrap_Bootstrap in your Bootstrap class and catch the exceptions thrown during the bootstrap process or while running the application and call your exception handler as shown below.

        protected function _bootstrap($resource = null)
        {
            try {
                parent::_bootstrap($resource);
            } catch(Exception $e) {
                $this->__handleExecptions($e);
            }
        }
    
        public function run()
        {
            try {
                parent::run();
            } catch(Exception $e) {
                $this->__handleExecptions($e);
            }
        }
    

    This should handle all your exceptions. For handling errors register your own error handler as shown below.

    public function _initErrorHandler(){
            set_error_handler(array($this, '__hanleErrors'));
    }
    
    public function __hanleErrors($errNo, $errStr, $errFile, $errLine){
            //render a view with a simple error message for the user
            //and send an email with the error to admin
        }
    

    Now as you manually handle both errors and exceptions you can show user a simple message (by rendering a view) and email technical details to admin.

    Hope this helps.

    评论

报告相同问题?