Exception.php 1.44 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8 9
namespace yii\base;

Qiang Xue committed
10 11 12 13
/**
 * Exception represents a generic exception for all purposes.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
14
 * @since 2.0
Qiang Xue committed
15
 */
16
class Exception extends \Exception implements Arrayable
Qiang Xue committed
17
{
Qiang Xue committed
18 19 20 21 22
	/**
	 * @return string the user-friendly name of this exception
	 */
	public function getName()
	{
23
		return \Yii::t('yii', 'Exception');
Qiang Xue committed
24
	}
25 26 27 28 29 30 31

	/**
	 * Returns the array representation of this object.
	 * @return array the array representation of this object.
	 */
	public function toArray()
	{
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
		return $this->toArrayRecursive($this);
	}

	/**
	 * Returns the array representation of the exception and all previous exceptions recursively.
	 * @param \Exception exception object
	 * @return array the array representation of the exception.
	 */
	protected function toArrayRecursive($exception)
	{
		if ($exception instanceof self) {
			$array = array(
				'type' => get_class($this),
				'name' => $this->getName(),
				'message' => $this->getMessage(),
				'code' => $this->getCode(),
			);
		} else {
			$array = array(
				'type' => get_class($exception),
				'name' => 'Exception',
				'message' => $exception->getMessage(),
				'code' => $exception->getCode(),
			);
		}
		if (($prev = $exception->getPrevious()) !== null) {
			$array['previous'] = $this->toArrayRecursive($prev);
59 60
		}
		return $array;
61
	}
Zander Baldwin committed
62
}