]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Exceptions/TException.php
Add Baculum
[bacula/bacula] / gui / baculum / framework / Exceptions / TException.php
1 <?php
2 /**
3  * Exception classes file
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link http://www.pradosoft.com/
7  * @copyright Copyright &copy; 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
11  */
12
13 /**
14  * TException class
15  *
16  * TException is the base class for all PRADO exceptions.
17  *
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
22  * information.
23  *
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.
29  *
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
33  * @since 3.0
34  */
35 class TException extends Exception
36 {
37         private $_errorCode='';
38         static $_messageCache=array();
39
40         /**
41          * Constructor.
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.
46          */
47         public function __construct($errorMessage)
48         {
49                 $this->_errorCode=$errorMessage;
50                 $errorMessage=$this->translateErrorMessage($errorMessage);
51                 $args=func_get_args();
52                 array_shift($args);
53                 $n=count($args);
54                 $tokens=array();
55                 for($i=0;$i<$n;++$i)
56                         $tokens['{'.$i.'}']=TPropertyValue::ensureString($args[$i]);
57                 parent::__construct(strtr($errorMessage,$tokens));
58         }
59
60         /**
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
64          */
65         protected function translateErrorMessage($key)
66         {
67                 $msgFile=$this->getErrorMessageFile();
68
69                 // Cache messages
70                 if (!isset(self::$_messageCache[$msgFile])) 
71                 {
72                         if(($entries=@file($msgFile))!==false)
73                         {
74                                 foreach($entries as $entry)
75                                 {
76                                         @list($code,$message)=explode('=',$entry,2);
77                                         self::$_messageCache[$msgFile][trim($code)]=trim($message);
78                                 }
79                         }
80                 }
81                 return isset(self::$_messageCache[$msgFile][$key]) ? self::$_messageCache[$msgFile][$key] : $key;
82         }
83
84         /**
85          * @return string path to the error message file
86          */
87         protected function getErrorMessageFile()
88         {
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';
93                 return $msgFile;
94         }
95
96         /**
97          * @return string error code
98          */
99         public function getErrorCode()
100         {
101                 return $this->_errorCode;
102         }
103
104         /**
105          * @param string error code
106          */
107         public function setErrorCode($code)
108         {
109                 $this->_errorCode=$code;
110         }
111
112         /**
113          * @return string error message
114          */
115         public function getErrorMessage()
116         {
117                 return $this->getMessage();
118         }
119
120         /**
121          * @param string error message
122          */
123         protected function setErrorMessage($message)
124         {
125                 $this->message=$message;
126         }
127 }
128
129 /**
130  * TSystemException class
131  *
132  * TSystemException is the base class for all framework-level exceptions.
133  *
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
137  * @since 3.0
138  */
139 class TSystemException extends TException
140 {
141 }
142
143 /**
144  * TApplicationException class
145  *
146  * TApplicationException is the base class for all user application-level exceptions.
147  *
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
151  * @since 3.0
152  */
153 class TApplicationException extends TException
154 {
155 }
156
157 /**
158  * TInvalidOperationException class
159  *
160  * TInvalidOperationException represents an exception caused by invalid operations.
161  *
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
165  * @since 3.0
166  */
167 class TInvalidOperationException extends TSystemException
168 {
169 }
170
171 /**
172  * TInvalidDataTypeException class
173  *
174  * TInvalidDataTypeException represents an exception caused by invalid data type.
175  *
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
179  * @since 3.0
180  */
181 class TInvalidDataTypeException extends TSystemException
182 {
183 }
184
185 /**
186  * TInvalidDataValueException class
187  *
188  * TInvalidDataValueException represents an exception caused by invalid data value.
189  *
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
193  * @since 3.0
194  */
195 class TInvalidDataValueException extends TSystemException
196 {
197 }
198
199 /**
200  * TConfigurationException class
201  *
202  * TConfigurationException represents an exception caused by invalid configurations,
203  * such as error in an application configuration file or control template file.
204  *
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
208  * @since 3.0
209  */
210 class TConfigurationException extends TSystemException
211 {
212 }
213
214 /**
215  * TTemplateException class
216  *
217  * TTemplateException represents an exception caused by invalid template syntax.
218  *
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
222  * @since 3.1
223  */
224 class TTemplateException extends TConfigurationException
225 {
226         private $_template='';
227         private $_lineNumber=0;
228         private $_fileName='';
229
230         /**
231          * @return string the template source code that causes the exception. This is empty if {@link getTemplateFile TemplateFile} is not empty.
232          */
233         public function getTemplateSource()
234         {
235                 return $this->_template;
236         }
237
238         /**
239          * @param string the template source code that causes the exception
240          */
241         public function setTemplateSource($value)
242         {
243                 $this->_template=$value;
244         }
245
246         /**
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.
248          */
249         public function getTemplateFile()
250         {
251                 return $this->_fileName;
252         }
253
254         /**
255          * @param string the template file that causes the exception
256          */
257         public function setTemplateFile($value)
258         {
259                 $this->_fileName=$value;
260         }
261
262         /**
263          * @return integer the line number at which the template has error
264          */
265         public function getLineNumber()
266         {
267                 return $this->_lineNumber;
268         }
269
270         /**
271          * @param integer the line number at which the template has error
272          */
273         public function setLineNumber($value)
274         {
275                 $this->_lineNumber=TPropertyValue::ensureInteger($value);
276         }
277 }
278
279 /**
280  * TIOException class
281  *
282  * TIOException represents an exception related with improper IO operations.
283  *
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
287  * @since 3.0
288  */
289 class TIOException extends TSystemException
290 {
291 }
292
293 /**
294  * TDbException class
295  *
296  * TDbException represents an exception related with DB operations.
297  *
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
301  * @since 3.0
302  */
303 class TDbException extends TSystemException
304 {
305 }
306
307 /**
308  * TDbConnectionException class
309  *
310  * TDbConnectionException represents an exception caused by DB connection failure.
311  *
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
315  * @since 3.0
316  */
317 class TDbConnectionException extends TDbException
318 {
319 }
320
321 /**
322  * TNotSupportedException class
323  *
324  * TNotSupportedException represents an exception caused by using an unsupported PRADO feature.
325  *
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
329  * @since 3.0
330  */
331 class TNotSupportedException extends TSystemException
332 {
333 }
334
335 /**
336  * TPhpErrorException class
337  *
338  * TPhpErrorException represents an exception caused by a PHP error.
339  * This exception is mainly thrown within a PHP error handler.
340  *
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
344  * @since 3.0
345  */
346 class TPhpErrorException extends TSystemException
347 {
348         /**
349          * Constructor.
350          * @param integer error number
351          * @param string error string
352          * @param string error file
353          * @param integer error line number
354          */
355         public function __construct($errno,$errstr,$errfile,$errline)
356         {
357                 static $errorTypes=array(
358                         E_ERROR           => "Error",
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"
370                 );
371                 $errorType=isset($errorTypes[$errno])?$errorTypes[$errno]:'Unknown Error';
372                 parent::__construct("[$errorType] $errstr (@line $errline in file $errfile).");
373         }
374 }
375
376
377 /**
378  * THttpException class
379  *
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.
383  *
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
387  * @since 3.0
388  */
389 class THttpException extends TSystemException
390 {
391         private $_statusCode;
392
393         /**
394          * Constructor.
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.
400          */
401         public function __construct($statusCode,$errorMessage)
402         {
403                 $this->_statusCode=$statusCode;
404                 $this->setErrorCode($errorMessage);
405                 $errorMessage=$this->translateErrorMessage($errorMessage);
406                 $args=func_get_args();
407                 array_shift($args);
408                 array_shift($args);
409                 $n=count($args);
410                 $tokens=array();
411                 for($i=0;$i<$n;++$i)
412                         $tokens['{'.$i.'}']=TPropertyValue::ensureString($args[$i]);
413                 parent::__construct(strtr($errorMessage,$tokens));
414         }
415
416         /**
417          * @return integer HTTP status code, such as 404, 500, etc.
418          */
419         public function getStatusCode()
420         {
421                 return $this->_statusCode;
422         }
423 }
424