Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
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 2541 2008-10-21 15:05:13Z qiang.xue $
32
 * @package System.Exceptions
33
 * @since 3.0
34
 */
35
class TException extends Exception
36
{
37
	private $_errorCode='';
38
 
39
	/**
40
	 * Constructor.
41
	 * @param string error message. This can be a string that is listed
42
	 * in the message file. If so, the message in the preferred language
43
	 * will be used as the error message. Any rest parameters will be used
44
	 * to replace placeholders ({0}, {1}, {2}, etc.) in the message.
45
	 */
46
	public function __construct($errorMessage)
47
	{
48
		$this->_errorCode=$errorMessage;
49
		$errorMessage=$this->translateErrorMessage($errorMessage);
50
		$args=func_get_args();
51
		array_shift($args);
52
		$n=count($args);
53
		$tokens=array();
54
		for($i=0;$i<$n;++$i)
55
			$tokens['{'.$i.'}']=TPropertyValue::ensureString($args[$i]);
56
		parent::__construct(strtr($errorMessage,$tokens));
57
	}
58
 
59
	/**
60
	 * Translates an error code into an error message.
61
	 * @param string error code that is passed in the exception constructor.
62
	 * @return string the translated error message
63
	 */
64
	protected function translateErrorMessage($key)
65
	{
66
		$msgFile=$this->getErrorMessageFile();
67
		if(($entries=@file($msgFile))!==false)
68
		{
69
			foreach($entries as $entry)
70
			{
71
				@list($code,$message)=explode('=',$entry,2);
72
				if(trim($code)===$key)
73
					return trim($message);
74
			}
75
		}
76
		return $key;
77
	}
78
 
79
	/**
80
	 * @return string path to the error message file
81
	 */
82
	protected function getErrorMessageFile()
83
	{
84
		$lang=Prado::getPreferredLanguage();
85
		$msgFile=Prado::getFrameworkPath().'/Exceptions/messages/messages-'.$lang.'.txt';
86
		if(!is_file($msgFile))
87
			$msgFile=Prado::getFrameworkPath().'/Exceptions/messages/messages.txt';
88
		return $msgFile;
89
	}
90
 
91
	/**
92
	 * @return string error code
93
	 */
94
	public function getErrorCode()
95
	{
96
		return $this->_errorCode;
97
	}
98
 
99
	/**
100
	 * @param string error code
101
	 */
102
	public function setErrorCode($code)
103
	{
104
		$this->_errorCode=$code;
105
	}
106
 
107
	/**
108
	 * @return string error message
109
	 */
110
	public function getErrorMessage()
111
	{
112
		return $this->getMessage();
113
	}
114
 
115
	/**
116
	 * @param string error message
117
	 */
118
	protected function setErrorMessage($message)
119
	{
120
		$this->message=$message;
121
	}
122
}
123
 
124
/**
125
 * TSystemException class
126
 *
127
 * TSystemException is the base class for all framework-level exceptions.
128
 *
129
 * @author Qiang Xue <qiang.xue@gmail.com>
130
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
131
 * @package System.Exceptions
132
 * @since 3.0
133
 */
134
class TSystemException extends TException
135
{
136
}
137
 
138
/**
139
 * TApplicationException class
140
 *
141
 * TApplicationException is the base class for all user application-level exceptions.
142
 *
143
 * @author Qiang Xue <qiang.xue@gmail.com>
144
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
145
 * @package System.Exceptions
146
 * @since 3.0
147
 */
148
class TApplicationException extends TException
149
{
150
}
151
 
152
/**
153
 * TInvalidOperationException class
154
 *
155
 * TInvalidOperationException represents an exception caused by invalid operations.
156
 *
157
 * @author Qiang Xue <qiang.xue@gmail.com>
158
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
159
 * @package System.Exceptions
160
 * @since 3.0
161
 */
162
class TInvalidOperationException extends TSystemException
163
{
164
}
165
 
166
/**
167
 * TInvalidDataTypeException class
168
 *
169
 * TInvalidDataTypeException represents an exception caused by invalid data type.
170
 *
171
 * @author Qiang Xue <qiang.xue@gmail.com>
172
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
173
 * @package System.Exceptions
174
 * @since 3.0
175
 */
176
class TInvalidDataTypeException extends TSystemException
177
{
178
}
179
 
180
/**
181
 * TInvalidDataValueException class
182
 *
183
 * TInvalidDataValueException represents an exception caused by invalid data value.
184
 *
185
 * @author Qiang Xue <qiang.xue@gmail.com>
186
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
187
 * @package System.Exceptions
188
 * @since 3.0
189
 */
190
class TInvalidDataValueException extends TSystemException
191
{
192
}
193
 
194
/**
195
 * TConfigurationException class
196
 *
197
 * TConfigurationException represents an exception caused by invalid configurations,
198
 * such as error in an application configuration file or control template file.
199
 *
200
 * @author Qiang Xue <qiang.xue@gmail.com>
201
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
202
 * @package System.Exceptions
203
 * @since 3.0
204
 */
205
class TConfigurationException extends TSystemException
206
{
207
}
208
 
209
/**
210
 * TTemplateException class
211
 *
212
 * TTemplateException represents an exception caused by invalid template syntax.
213
 *
214
 * @author Qiang Xue <qiang.xue@gmail.com>
215
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
216
 * @package System.Exceptions
217
 * @since 3.1
218
 */
219
class TTemplateException extends TConfigurationException
220
{
221
	private $_template='';
222
	private $_lineNumber=0;
223
	private $_fileName='';
224
 
225
	/**
226
	 * @return string the template source code that causes the exception. This is empty if {@link getTemplateFile TemplateFile} is not empty.
227
	 */
228
	public function getTemplateSource()
229
	{
230
		return $this->_template;
231
	}
232
 
233
	/**
234
	 * @param string the template source code that causes the exception
235
	 */
236
	public function setTemplateSource($value)
237
	{
238
		$this->_template=$value;
239
	}
240
 
241
	/**
242
	 * @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.
243
	 */
244
	public function getTemplateFile()
245
	{
246
		return $this->_fileName;
247
	}
248
 
249
	/**
250
	 * @param string the template file that causes the exception
251
	 */
252
	public function setTemplateFile($value)
253
	{
254
		$this->_fileName=$value;
255
	}
256
 
257
	/**
258
	 * @return integer the line number at which the template has error
259
	 */
260
	public function getLineNumber()
261
	{
262
		return $this->_lineNumber;
263
	}
264
 
265
	/**
266
	 * @param integer the line number at which the template has error
267
	 */
268
	public function setLineNumber($value)
269
	{
270
		$this->_lineNumber=TPropertyValue::ensureInteger($value);
271
	}
272
}
273
 
274
/**
275
 * TIOException class
276
 *
277
 * TIOException represents an exception related with improper IO operations.
278
 *
279
 * @author Qiang Xue <qiang.xue@gmail.com>
280
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
281
 * @package System.Exceptions
282
 * @since 3.0
283
 */
284
class TIOException extends TSystemException
285
{
286
}
287
 
288
/**
289
 * TDbException class
290
 *
291
 * TDbException represents an exception related with DB operations.
292
 *
293
 * @author Qiang Xue <qiang.xue@gmail.com>
294
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
295
 * @package System.Exceptions
296
 * @since 3.0
297
 */
298
class TDbException extends TSystemException
299
{
300
}
301
 
302
/**
303
 * TDbConnectionException class
304
 *
305
 * TDbConnectionException represents an exception caused by DB connection failure.
306
 *
307
 * @author Qiang Xue <qiang.xue@gmail.com>
308
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
309
 * @package System.Exceptions
310
 * @since 3.0
311
 */
312
class TDbConnectionException extends TDbException
313
{
314
}
315
 
316
/**
317
 * TNotSupportedException class
318
 *
319
 * TNotSupportedException represents an exception caused by using an unsupported PRADO feature.
320
 *
321
 * @author Qiang Xue <qiang.xue@gmail.com>
322
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
323
 * @package System.Exceptions
324
 * @since 3.0
325
 */
326
class TNotSupportedException extends TSystemException
327
{
328
}
329
 
330
/**
331
 * TPhpErrorException class
332
 *
333
 * TPhpErrorException represents an exception caused by a PHP error.
334
 * This exception is mainly thrown within a PHP error handler.
335
 *
336
 * @author Qiang Xue <qiang.xue@gmail.com>
337
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
338
 * @package System.Exceptions
339
 * @since 3.0
340
 */
341
class TPhpErrorException extends TSystemException
342
{
343
	/**
344
	 * Constructor.
345
	 * @param integer error number
346
	 * @param string error string
347
	 * @param string error file
348
	 * @param integer error line number
349
	 */
350
	public function __construct($errno,$errstr,$errfile,$errline)
351
	{
352
		static $errorTypes=array(
353
			E_ERROR           => "Error",
354
			E_WARNING         => "Warning",
355
			E_PARSE           => "Parsing Error",
356
			E_NOTICE          => "Notice",
357
			E_CORE_ERROR      => "Core Error",
358
			E_CORE_WARNING    => "Core Warning",
359
			E_COMPILE_ERROR   => "Compile Error",
360
			E_COMPILE_WARNING => "Compile Warning",
361
			E_USER_ERROR      => "User Error",
362
			E_USER_WARNING    => "User Warning",
363
			E_USER_NOTICE     => "User Notice",
364
			E_STRICT          => "Runtime Notice"
365
		);
366
		$errorType=isset($errorTypes[$errno])?$errorTypes[$errno]:'Unknown Error';
367
		parent::__construct("[$errorType] $errstr (@line $errline in file $errfile).");
368
	}
369
}
370
 
371
 
372
/**
373
 * THttpException class
374
 *
375
 * THttpException represents an exception that is caused by invalid operations
376
 * of end-users. The {@link getStatusCode StatusCode} gives the type of HTTP error.
377
 * It is used by {@link TErrorHandler} to provide different error output to users.
378
 *
379
 * @author Qiang Xue <qiang.xue@gmail.com>
380
 * @version $Id: TException.php 2541 2008-10-21 15:05:13Z qiang.xue $
381
 * @package System.Exceptions
382
 * @since 3.0
383
 */
384
class THttpException extends TSystemException
385
{
386
	private $_statusCode;
387
 
388
	/**
389
	 * Constructor.
390
	 * @param integer HTTP status code, such as 404, 500, etc.
391
	 * @param string error message. This can be a string that is listed
392
	 * in the message file. If so, the message in the preferred language
393
	 * will be used as the error message. Any rest parameters will be used
394
	 * to replace placeholders ({0}, {1}, {2}, etc.) in the message.
395
	 */
396
	public function __construct($statusCode,$errorMessage)
397
	{
398
		$this->_statusCode=$statusCode;
399
		$this->setErrorCode($errorMessage);
400
		$errorMessage=$this->translateErrorMessage($errorMessage);
401
		$args=func_get_args();
402
		array_shift($args);
403
		array_shift($args);
404
		$n=count($args);
405
		$tokens=array();
406
		for($i=0;$i<$n;++$i)
407
			$tokens['{'.$i.'}']=TPropertyValue::ensureString($args[$i]);
408
		parent::__construct(strtr($errorMessage,$tokens));
409
	}
410
 
411
	/**
412
	 * @return integer HTTP status code, such as 404, 500, etc.
413
	 */
414
	public function getStatusCode()
415
	{
416
		return $this->_statusCode;
417
	}
418
}
419