+ * @version $Revision: $ $Date: $
+ * @package System.Exceptions
+ * @since 3.0
+ */
class TErrorHandler extends TComponent implements IModule
{
+ /**
+ * error template file basename
+ */
const ERROR_FILE_NAME='error';
+ /**
+ * exception template file basename
+ */
const EXCEPTION_FILE_NAME='exception';
/**
@@ -52,6 +107,12 @@ class TErrorHandler extends TComponent implements IModule
return $this->_templatePath;
}
+ /**
+ * Sets the path storing all error and exception template files.
+ * The path must be in namespace format, such as System.Exceptions (which is the default).
+ * @param string template path in namespace format
+ * @throws TConfigurationException if the template path is invalid
+ */
public function setErrorTemplatePath($value)
{
if(($templatePath=Prado::getPathOfNamespace($this->_templatePath))!==null && is_dir($templatePath))
@@ -60,15 +121,25 @@ class TErrorHandler extends TComponent implements IModule
throw new TConfigurationException('errorhandler_errortemplatepath_invalid',$value);
}
+ /**
+ * Handles PHP user errors and exceptions.
+ * This is the event handler responding to the Error event
+ * raised in {@link TApplication}.
+ * The method mainly uses appropriate template to display the error/exception.
+ * It terminates the application immediately after the error is displayed.
+ * @param mixed sender of the event
+ * @param mixed event parameter (if the event is raised by TApplication, it refers to the exception instance)
+ */
public function handleError($sender,$param)
{
static $handling=false;
// We need to restore error and exception handlers,
- // otherwise because errors occured in error handler
- // will not be handled properly.
+ // because within error and exception handlers, new errors and exceptions
+ // cannot be handled properly by PHP
restore_error_handler();
restore_exception_handler();
- if($handling) // ensure that we do not enter infinite loop of error handling
+ // ensure that we do not enter infinite loop of error handling
+ if($handling)
$this->handleRecursiveError($param);
else
{
@@ -85,6 +156,13 @@ class TErrorHandler extends TComponent implements IModule
exit(1);
}
+ /**
+ * Displays error to the client user.
+ * THttpException and errors happened when the application is in Debug
+ * mode will be displayed to the client user.
+ * @param integer response status code
+ * @param Exception exception instance
+ */
protected function handleExternalError($statusCode,$exception)
{
if(!($exception instanceof THttpException))
@@ -122,6 +200,13 @@ class TErrorHandler extends TComponent implements IModule
echo str_replace($fields,$values,$content);
}
+ /**
+ * Handles error occurs during error handling (called recursive error).
+ * THttpException and errors happened when the application is in Debug
+ * mode will be displayed to the client user.
+ * Error is displayed without using existing template to prevent further errors.
+ * @param Exception exception instance
+ */
protected function handleRecursiveError($exception)
{
if(Prado::getApplication()->getMode()==='Debug')
@@ -138,6 +223,13 @@ class TErrorHandler extends TComponent implements IModule
}
}
+ /**
+ * Displays exception information.
+ * Exceptions are displayed with rich context information, including
+ * the call stack and the context source code.
+ * This method is only invoked when application is in Debug mode.
+ * @param Exception exception instance
+ */
protected function displayException($exception)
{
$lines=file($exception->getFile());
@@ -174,9 +266,9 @@ class TErrorHandler extends TComponent implements IModule
strftime('%Y-%m-%d %H:%m',time())
);
$lang=Prado::getPreferredLanguage();
- $exceptionFile=dirname(__FILE__).'/'.self::EXCEPTION_FILE_NAME.'-'.$lang.'.html';
+ $exceptionFile=$this->_templatePath.'/'.self::EXCEPTION_FILE_NAME.'-'.$lang.'.html';
if(!is_file($exceptionFile))
- $exceptionFile=dirname(__FILE__).'/'.self::EXCEPTION_FILE_NAME.'.html';
+ $exceptionFile=$this->_templatePath.'/'.self::EXCEPTION_FILE_NAME.'.html';
if(($content=@file_get_contents($exceptionFile))===false)
die("Unable to open exception template file '$exceptionFile'.");
echo str_replace($fields,$values,$content);
diff --git a/framework/Exceptions/error503-en.html b/framework/Exceptions/error503-en.html
new file mode 100644
index 00000000..60527c45
--- /dev/null
+++ b/framework/Exceptions/error503-en.html
@@ -0,0 +1,27 @@
+
+
+Service Unavailable
+
+
+
+
+Service Unavailable
+
+Our system is currently under maintenance. Please come back later.
+
+
+Thank you.
+
+
+%%Version%%
+%%Time%%
+
+
+
\ No newline at end of file
diff --git a/framework/Exceptions/error503-zh.html b/framework/Exceptions/error503-zh.html
new file mode 100644
index 00000000..b6b5915c
--- /dev/null
+++ b/framework/Exceptions/error503-zh.html
@@ -0,0 +1,27 @@
+
+
+系统无法提供服务
+
+
+
+
+系统无法提供服务
+
+系统维护中,请稍后再来访问。
+
+
+谢谢。
+
+
+%%Version%%
+%%Time%%
+
+
+
\ No newline at end of file
diff --git a/framework/Exceptions/error503.html b/framework/Exceptions/error503.html
new file mode 100644
index 00000000..60527c45
--- /dev/null
+++ b/framework/Exceptions/error503.html
@@ -0,0 +1,27 @@
+
+
+Service Unavailable
+
+
+
+
+Service Unavailable
+
+Our system is currently under maintenance. Please come back later.
+
+
+Thank you.
+
+
+%%Version%%
+%%Time%%
+
+
+
\ No newline at end of file
diff --git a/framework/Exceptions/messages.txt b/framework/Exceptions/messages.txt
index 9ed1160b..17e1e627 100644
--- a/framework/Exceptions/messages.txt
+++ b/framework/Exceptions/messages.txt
@@ -11,6 +11,7 @@ application_configfile_inexistent = Application configuration file '%s' does no
application_module_existing = Application module '%s' cannot be registered twice.
application_service_invalid = Service '%s' must implement IService interface.
application_service_unknown = Requested service '%s' is not defined.
+application_service_unavailable = Service Unavailable.
appconfig_aliaspath_invalid = Application configuration uses an invalid file path "%s".
appconfig_aliasid_required = Application configuration element must have an "id" attribute.
@@ -55,6 +56,8 @@ memcache_connection_failed = TMemCache failed to connect to memcache server %
memcache_host_unchangeable = TMemCache.Host cannot be modified after the module is initialized.
memcache_port_unchangeable = TMemCache.Port cannot be modified after the module is initialized.
+errorhandler_errortemplatepath_invalid = TErrorHandler.ErrorTemplatePath '%s' is invalid. Make sure it is in namespace form and points to a valid directory containing error template files.
+
pageservice_page_unknown = Page '%s' Not Found
--
cgit v1.2.3