3 * Exception classes file
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2013 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
10 * @package System.Exceptions
16 * TException is the base class for all PRADO exceptions.
18 * TException provides the functionality of translating an error code
19 * into a descriptive error message in a language that is preferred
20 * by user browser. Additional parameters may be passed together with
21 * the error code so that the translated message contains more detailed
24 * By default, TException looks for a message file by calling
25 * {@link getErrorMessageFile()} method, which uses the "message-xx.txt"
26 * file located under "System.Exceptions" folder, where "xx" is the
27 * code of the user preferred language. If such a file is not found,
28 * "message.txt" will be used instead.
30 * @author Qiang Xue <qiang.xue@gmail.com>
31 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
32 * @package System.Exceptions
35 class TException extends Exception
37 private $_errorCode='';
38 static $_messageCache=array();
42 * @param string error message. This can be a string that is listed
43 * in the message file. If so, the message in the preferred language
44 * will be used as the error message. Any rest parameters will be used
45 * to replace placeholders ({0}, {1}, {2}, etc.) in the message.
47 public function __construct($errorMessage)
49 $this->_errorCode=$errorMessage;
50 $errorMessage=$this->translateErrorMessage($errorMessage);
51 $args=func_get_args();
56 $tokens['{'.$i.'}']=TPropertyValue::ensureString($args[$i]);
57 parent::__construct(strtr($errorMessage,$tokens));
61 * Translates an error code into an error message.
62 * @param string error code that is passed in the exception constructor.
63 * @return string the translated error message
65 protected function translateErrorMessage($key)
67 $msgFile=$this->getErrorMessageFile();
70 if (!isset(self::$_messageCache[$msgFile]))
72 if(($entries=@file($msgFile))!==false)
74 foreach($entries as $entry)
76 @list($code,$message)=explode('=',$entry,2);
77 self::$_messageCache[$msgFile][trim($code)]=trim($message);
81 return isset(self::$_messageCache[$msgFile][$key]) ? self::$_messageCache[$msgFile][$key] : $key;
85 * @return string path to the error message file
87 protected function getErrorMessageFile()
89 $lang=Prado::getPreferredLanguage();
90 $msgFile=Prado::getFrameworkPath().'/Exceptions/messages/messages-'.$lang.'.txt';
91 if(!is_file($msgFile))
92 $msgFile=Prado::getFrameworkPath().'/Exceptions/messages/messages.txt';
97 * @return string error code
99 public function getErrorCode()
101 return $this->_errorCode;
105 * @param string error code
107 public function setErrorCode($code)
109 $this->_errorCode=$code;
113 * @return string error message
115 public function getErrorMessage()
117 return $this->getMessage();
121 * @param string error message
123 protected function setErrorMessage($message)
125 $this->message=$message;
130 * TSystemException class
132 * TSystemException is the base class for all framework-level exceptions.
134 * @author Qiang Xue <qiang.xue@gmail.com>
135 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
136 * @package System.Exceptions
139 class TSystemException extends TException
144 * TApplicationException class
146 * TApplicationException is the base class for all user application-level exceptions.
148 * @author Qiang Xue <qiang.xue@gmail.com>
149 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
150 * @package System.Exceptions
153 class TApplicationException extends TException
158 * TInvalidOperationException class
160 * TInvalidOperationException represents an exception caused by invalid operations.
162 * @author Qiang Xue <qiang.xue@gmail.com>
163 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
164 * @package System.Exceptions
167 class TInvalidOperationException extends TSystemException
172 * TInvalidDataTypeException class
174 * TInvalidDataTypeException represents an exception caused by invalid data type.
176 * @author Qiang Xue <qiang.xue@gmail.com>
177 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
178 * @package System.Exceptions
181 class TInvalidDataTypeException extends TSystemException
186 * TInvalidDataValueException class
188 * TInvalidDataValueException represents an exception caused by invalid data value.
190 * @author Qiang Xue <qiang.xue@gmail.com>
191 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
192 * @package System.Exceptions
195 class TInvalidDataValueException extends TSystemException
200 * TConfigurationException class
202 * TConfigurationException represents an exception caused by invalid configurations,
203 * such as error in an application configuration file or control template file.
205 * @author Qiang Xue <qiang.xue@gmail.com>
206 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
207 * @package System.Exceptions
210 class TConfigurationException extends TSystemException
215 * TTemplateException class
217 * TTemplateException represents an exception caused by invalid template syntax.
219 * @author Qiang Xue <qiang.xue@gmail.com>
220 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
221 * @package System.Exceptions
224 class TTemplateException extends TConfigurationException
226 private $_template='';
227 private $_lineNumber=0;
228 private $_fileName='';
231 * @return string the template source code that causes the exception. This is empty if {@link getTemplateFile TemplateFile} is not empty.
233 public function getTemplateSource()
235 return $this->_template;
239 * @param string the template source code that causes the exception
241 public function setTemplateSource($value)
243 $this->_template=$value;
247 * @return string the template file that causes the exception. This could be empty if the template is an embedded template. In this case, use {@link getTemplateSource TemplateSource} to obtain the actual template content.
249 public function getTemplateFile()
251 return $this->_fileName;
255 * @param string the template file that causes the exception
257 public function setTemplateFile($value)
259 $this->_fileName=$value;
263 * @return integer the line number at which the template has error
265 public function getLineNumber()
267 return $this->_lineNumber;
271 * @param integer the line number at which the template has error
273 public function setLineNumber($value)
275 $this->_lineNumber=TPropertyValue::ensureInteger($value);
282 * TIOException represents an exception related with improper IO operations.
284 * @author Qiang Xue <qiang.xue@gmail.com>
285 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
286 * @package System.Exceptions
289 class TIOException extends TSystemException
296 * TDbException represents an exception related with DB operations.
298 * @author Qiang Xue <qiang.xue@gmail.com>
299 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
300 * @package System.Exceptions
303 class TDbException extends TSystemException
308 * TDbConnectionException class
310 * TDbConnectionException represents an exception caused by DB connection failure.
312 * @author Qiang Xue <qiang.xue@gmail.com>
313 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
314 * @package System.Exceptions
317 class TDbConnectionException extends TDbException
322 * TNotSupportedException class
324 * TNotSupportedException represents an exception caused by using an unsupported PRADO feature.
326 * @author Qiang Xue <qiang.xue@gmail.com>
327 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
328 * @package System.Exceptions
331 class TNotSupportedException extends TSystemException
336 * TPhpErrorException class
338 * TPhpErrorException represents an exception caused by a PHP error.
339 * This exception is mainly thrown within a PHP error handler.
341 * @author Qiang Xue <qiang.xue@gmail.com>
342 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
343 * @package System.Exceptions
346 class TPhpErrorException extends TSystemException
350 * @param integer error number
351 * @param string error string
352 * @param string error file
353 * @param integer error line number
355 public function __construct($errno,$errstr,$errfile,$errline)
357 static $errorTypes=array(
359 E_WARNING => "Warning",
360 E_PARSE => "Parsing Error",
361 E_NOTICE => "Notice",
362 E_CORE_ERROR => "Core Error",
363 E_CORE_WARNING => "Core Warning",
364 E_COMPILE_ERROR => "Compile Error",
365 E_COMPILE_WARNING => "Compile Warning",
366 E_USER_ERROR => "User Error",
367 E_USER_WARNING => "User Warning",
368 E_USER_NOTICE => "User Notice",
369 E_STRICT => "Runtime Notice"
371 $errorType=isset($errorTypes[$errno])?$errorTypes[$errno]:'Unknown Error';
372 parent::__construct("[$errorType] $errstr (@line $errline in file $errfile).");
378 * THttpException class
380 * THttpException represents an exception that is caused by invalid operations
381 * of end-users. The {@link getStatusCode StatusCode} gives the type of HTTP error.
382 * It is used by {@link TErrorHandler} to provide different error output to users.
384 * @author Qiang Xue <qiang.xue@gmail.com>
385 * @version $Id: TException.php 3245 2013-01-07 20:23:32Z ctrlaltca $
386 * @package System.Exceptions
389 class THttpException extends TSystemException
391 private $_statusCode;
395 * @param integer HTTP status code, such as 404, 500, etc.
396 * @param string error message. This can be a string that is listed
397 * in the message file. If so, the message in the preferred language
398 * will be used as the error message. Any rest parameters will be used
399 * to replace placeholders ({0}, {1}, {2}, etc.) in the message.
401 public function __construct($statusCode,$errorMessage)
403 $this->_statusCode=$statusCode;
404 $this->setErrorCode($errorMessage);
405 $errorMessage=$this->translateErrorMessage($errorMessage);
406 $args=func_get_args();
412 $tokens['{'.$i.'}']=TPropertyValue::ensureString($args[$i]);
413 parent::__construct(strtr($errorMessage,$tokens));
417 * @return integer HTTP status code, such as 404, 500, etc.
419 public function getStatusCode()
421 return $this->_statusCode;