Changeset 2040

Show
Ignore:
Timestamp:
06/28/2007 09:17:22 PM
Author:
xue
Message:

Added getErrorTemplate() and getExceptionTemplate().

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/framework/Exceptions/TErrorHandler.php

    r1959 r2040  
    7878        { 
    7979                $this->getApplication()->setErrorHandler($this); 
     80                if($this->_templatePath===null) 
     81                        $this->_templatePath=Prado::getFrameworkPath().'/Exceptions/templates'; 
    8082        } 
    8183 
     
    149151                if(!($exception instanceof THttpException)) 
    150152                        error_log($exception->__toString()); 
    151                 if($this->_templatePath===null) 
    152                         $this->_templatePath=Prado::getFrameworkPath().'/Exceptions/templates'; 
    153                 $base=$this->_templatePath.'/'.self::ERROR_FILE_NAME; 
    154                 $lang=Prado::getPreferredLanguage(); 
    155                 if(is_file("$base$statusCode-$lang.html")) 
    156                         $errorFile="$base$statusCode-$lang.html"; 
    157                 else if(is_file("$base$statusCode.html")) 
    158                         $errorFile="$base$statusCode.html"; 
    159                 else if(is_file("$base-$lang.html")) 
    160                         $errorFile="$base-$lang.html"; 
    161                 else 
    162                         $errorFile="$base.html"; 
    163                 if(($content=@file_get_contents($errorFile))===false) 
    164                         die("Unable to open error template file '$errorFile'."); 
     153 
     154                $content=$this->getErrorTemplate($statusCode,$exception); 
    165155 
    166156                $serverAdmin=isset($_SERVER['SERVER_ADMIN'])?$_SERVER['SERVER_ADMIN']:''; 
     
    247237                        '%%Time%%' => @strftime('%Y-%m-%d %H:%M',time()) 
    248238                ); 
     239 
     240                $content=$this->getExceptionTemplate($exception); 
     241 
     242                echo strtr($content,$tokens); 
     243        } 
     244 
     245        /** 
     246         * Retrieves the template used for displaying internal exceptions. 
     247         * Internal exceptions will be displayed with source code causing the exception. 
     248         * This occurs when the application is in debug mode. 
     249         * @param Exception the exception to be displayed 
     250         * @return string the template content 
     251         */ 
     252        protected function getExceptionTemplate($exception) 
     253        { 
    249254                $lang=Prado::getPreferredLanguage(); 
    250255                $exceptionFile=Prado::getFrameworkPath().'/Exceptions/templates/'.self::EXCEPTION_FILE_NAME.'-'.$lang.'.html'; 
     
    253258                if(($content=@file_get_contents($exceptionFile))===false) 
    254259                        die("Unable to open exception template file '$exceptionFile'."); 
    255                 echo strtr($content,$tokens); 
     260                return $content; 
     261        } 
     262 
     263        /** 
     264         * Retrieves the template used for displaying external exceptions. 
     265         * External exceptions are those displayed to end-users. They do not contain 
     266         * error source code. Therefore, you might want to override this method 
     267         * to provide your own error template for displaying certain external exceptions. 
     268         * The following tokens in the template will be replaced with corresponding content: 
     269         * %%StatusCode%% : the status code of the exception 
     270         * %%ErrorMessage%% : the error message (HTML encoded). 
     271         * %%ServerAdmin%% : the server admin information (retrieved from Web server configuration) 
     272         * %%Version%% : the version information of the Web server. 
     273         * %%Time%% : the time the exception occurs at 
     274         * 
     275         * @param integer status code (such as 404, 500, etc.) 
     276         * @param Exception the exception to be displayed 
     277         * @return string the template content 
     278         */ 
     279        protected function getErrorTemplate($statusCode,$exception) 
     280        { 
     281                $base=$this->getErrorTemplatePath().'/'.self::ERROR_FILE_NAME; 
     282                $lang=Prado::getPreferredLanguage(); 
     283                if(is_file("$base$statusCode-$lang.html")) 
     284                        $errorFile="$base$statusCode-$lang.html"; 
     285                else if(is_file("$base$statusCode.html")) 
     286                        $errorFile="$base$statusCode.html"; 
     287                else if(is_file("$base-$lang.html")) 
     288                        $errorFile="$base-$lang.html"; 
     289                else 
     290                        $errorFile="$base.html"; 
     291                if(($content=@file_get_contents($errorFile))===false) 
     292                        die("Unable to open error template file '$errorFile'."); 
     293                return $content; 
    256294        } 
    257295