From 6b647694bdae2ab37246447cb965a5e7f97290ed Mon Sep 17 00:00:00 2001 From: xue <> Date: Fri, 18 Nov 2005 18:52:28 +0000 Subject: --- framework/Exceptions/TErrorHandler.php | 102 +++++++++++++++++++++++++++------ framework/Exceptions/TException.php | 41 +++++++------ framework/Exceptions/error.en | 27 +++------ framework/Exceptions/error404.en | 29 ++++++++++ framework/Exceptions/exception.en | 37 ++++++++++++ framework/Exceptions/messages.en | 3 + 6 files changed, 187 insertions(+), 52 deletions(-) create mode 100644 framework/Exceptions/error404.en create mode 100644 framework/Exceptions/exception.en (limited to 'framework/Exceptions') diff --git a/framework/Exceptions/TErrorHandler.php b/framework/Exceptions/TErrorHandler.php index 9af83d5e..687351d5 100644 --- a/framework/Exceptions/TErrorHandler.php +++ b/framework/Exceptions/TErrorHandler.php @@ -10,6 +10,10 @@ class TErrorHandler extends TComponent implements IModule * @var TApplication application instance */ private $_application; + /** + * @var array list of pages for displaying various HTTP errors + */ + private $_errorPages=array(); /** * Initializes the module. @@ -22,6 +26,8 @@ class TErrorHandler extends TComponent implements IModule $this->_application=$application; $application->attachEventHandler('Error',array($this,'handleError')); $application->setErrorHandler($this); + foreach($config->getElementsByTagName('error') as $node) + $this->_errorPages[$node->getAttribute('code')]=$node->getAttribute('page'); } /** @@ -42,19 +48,83 @@ class TErrorHandler extends TComponent implements IModule public function handleError($sender,$param) { - if(($response=Prado::getApplication()->getResponse())!==null) - $response->clear(); - switch(Prado::getApplication()->getMode()) + static $handling=false; + if($handling) + $this->handleRecursiveError($param); + else { - case 'Off': - case 'Debug': + $handling=true; + if(($response=Prado::getApplication()->getResponse())!==null) + $response->clear(); + if($param instanceof THttpException) + $this->handleExternalError($param->getStatusCode(),$param); + else if(Prado::getApplication()->getMode()==='Debug') $this->displayException($param); - exit(1); - case 'Normal': - case 'Performance': - error_log($param->__toString()); - header("HTTP/1.0 500 Internal Error"); - exit(1); + else + $this->handleExternalError(500,$param); + } + exit(1); + } + + protected function handleExternalError($statusCode,$exception) + { + if(!($exception instanceof THttpException)) + error_log($exception->__toString()); + if(isset($this->_errorPages["$statusCode"])) + { + $page=Prado::createComponent($this->_errorPages["$statusCode"]); + $writer=new THtmlTextWriter($this->_application->getResponse()); + $page->run($writer); + $writer->flush(); + } + else + { + $base=dirname(__FILE__).'/error'; + $languages=Prado::getUserLanguages(); + $lang=$languages[0]; + if(is_file("$base$statusCode.$lang")) + $errorFile="$base$statusCode.$lang"; + else if(is_file("$base$statusCode.en")) + $errorFile="$base$statusCode.en"; + else if(is_file("$base.$lang")) + $errorFile="$base.$lang"; + else + $errorFile="$base.en"; + if(($content=@file_get_contents($errorFile))===false) + die("Unable to open error template file '$errorFile'."); + + $serverAdmin=isset($_SERVER['SERVER_ADMIN'])?$_SERVER['SERVER_ADMIN']:''; + $fields=array( + '%%StatusCode%%', + '%%ErrorMessage%%', + '%%ServerAdmin%%', + '%%Version%%', + '%%Time%%' + ); + $values=array( + "$statusCode", + htmlspecialchars($exception->getMessage()), + $serverAdmin, + $_SERVER['SERVER_SOFTWARE'].' PRADO/'.Prado::getVersion(), + strftime('%Y-%m-%d %H:%m',time()) + ); + echo str_replace($fields,$values,$content); + } + } + + protected function handleRecursiveError($exception) + { + if(Prado::getApplication()->getMode()==='Debug') + { + echo "Recursive Error\n"; + echo "

Recursive Error

\n"; + echo "
".$exception."
\n"; + echo ""; + } + else + { + error_log("Error happened while processing an existing error:\n".$param->__toString()); + header('HTTP/1.0 500 Internal Error'); } } @@ -94,11 +164,11 @@ class TErrorHandler extends TComponent implements IModule strftime('%Y-%m-%d %H:%m',time()) ); $languages=Prado::getUserLanguages(); - $errorFile=dirname(__FILE__).'/error.'.$languages[0]; - if(!is_file($errorFile)) - $errorFile=dirname(__FILE__).'/error.en'; - if(($content=@file_get_contents($errorFile))===false) - die("Unable to open error template file '$errorFile'."); + $exceptionFile=dirname(__FILE__).'/exception.'.$languages[0]; + if(!is_file($exceptionFile)) + $exceptionFile=dirname(__FILE__).'/exception.en'; + if(($content=@file_get_contents($exceptionFile))===false) + die("Unable to open exception template file '$errorFile'."); echo str_replace($fields,$values,$content); } } diff --git a/framework/Exceptions/TException.php b/framework/Exceptions/TException.php index 0bc963f8..a90c14dd 100644 --- a/framework/Exceptions/TException.php +++ b/framework/Exceptions/TException.php @@ -40,16 +40,16 @@ class TException extends Exception { private $_errorCode=''; - public function __construct($errorCode) + public function __construct($errorMessage) { - $this->_errorCode=$errorCode; + $this->_errorCode=$errorMessage; $args=func_get_args(); - $args[0]=$this->translateErrorCode($errorCode); + $args[0]=$this->translateErrorMessage($errorMessage); $str=call_user_func_array('sprintf',$args); parent::__construct($str); } - protected function translateErrorCode($key) + protected function translateErrorMessage($key) { $languages=Prado::getUserLanguages(); $msgFile=dirname(__FILE__).'/messages.'.$languages[0]; @@ -74,20 +74,10 @@ class TException extends Exception return $this->_errorCode; } - public function setErrorCode($errorCode) - { - $this->_errorCode=$errorCode; - } - public function getErrorMessage() { return $this->getMessage(); } - - public function setErrorMessage($msg) - { - $this->message=$msg; - } } class TSystemException extends TException @@ -142,10 +132,6 @@ class TSecurityException extends TException { } -class THttpException extends TException -{ -} - class TNotSupportedException extends TException { } @@ -173,4 +159,23 @@ class TPhpErrorException extends TException } } + +class THttpException extends TException +{ + private $_statusCode; + + public function __construct($statusCode,$errorMessage) + { + $args=func_get_args(); + array_shift($args); + call_user_func_array(array('parent', '__construct'), $args); + $this->_statusCode=TPropertyValue::ensureInteger($statusCode); + } + + public function getStatusCode() + { + return $this->_statusCode; + } +} + ?> \ No newline at end of file diff --git a/framework/Exceptions/error.en b/framework/Exceptions/error.en index 1d76c7f2..cff0f910 100644 --- a/framework/Exceptions/error.en +++ b/framework/Exceptions/error.en @@ -1,34 +1,25 @@ -%%ErrorType%% +%%ErrorMessage%% -

%%ErrorType%%

-

Description

-

%%ErrorMessage%%

+

Error %%StatusCode%%

+

%%ErrorMessage%%

-

Source File

-

%%SourceFile%%

-
-%%SourceCode%% -
-

Stack Trace

-
-
-%%StackTrace%%
-
-
+The above error happened when the server was processing your request. +

+

+If you think this is a server error, please contact the webmaster. +

%%Version%%
%%Time%% diff --git a/framework/Exceptions/error404.en b/framework/Exceptions/error404.en new file mode 100644 index 00000000..11585661 --- /dev/null +++ b/framework/Exceptions/error404.en @@ -0,0 +1,29 @@ + + +Page Not Found + + + + +

%%ErrorMessage%%

+

Error 404

+

+The requested URL was not found on this server. +If you entered the URL manually please check your spelling and try again. +

+

+If you think this is a server error, please contact the webmaster. +

+
+%%Version%%
+%%Time%% +
+ + \ No newline at end of file diff --git a/framework/Exceptions/exception.en b/framework/Exceptions/exception.en new file mode 100644 index 00000000..1d76c7f2 --- /dev/null +++ b/framework/Exceptions/exception.en @@ -0,0 +1,37 @@ + + +%%ErrorType%% + + + + +

%%ErrorType%%

+

Description

+

%%ErrorMessage%%

+

+

Source File

+

%%SourceFile%%

+
+%%SourceCode%% +
+

Stack Trace

+
+
+%%StackTrace%%
+
+
+
+%%Version%%
+%%Time%% +
+ + \ No newline at end of file diff --git a/framework/Exceptions/messages.en b/framework/Exceptions/messages.en index 9c5dae1c..688901c2 100644 --- a/framework/Exceptions/messages.en +++ b/framework/Exceptions/messages.en @@ -43,6 +43,9 @@ assetmanager_basepath_invalid = TAssetManager.BasePath '%s' is invalid. Make s assetmanager_basepath_unchangeable = TAssetManager.BasePath cannot be modified after the module is initialized. assetmanager_baseurl_unchangeable = TAssetManager.BaseUrl cannot be modified after the module is initialized. +pageservice_page_unknown = Page not found: %s + + body_contents_not_allowed = %s: body contents are not allowed. control_id_not_unique = Control ID '%s' is not unique for control type '%s'. control_not_found = Unable to find a control with ID '%s'. -- cgit v1.2.3