mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-17 07:51:12 +08:00
Recursively return previous exceptions in toArray().
This commit is contained in:
@ -29,23 +29,33 @@ class Exception extends \Exception implements Arrayable
|
|||||||
*/
|
*/
|
||||||
public function toArray()
|
public function toArray()
|
||||||
{
|
{
|
||||||
$array = array(
|
return $this->toArrayRecursive($this);
|
||||||
'type' => get_class($this),
|
}
|
||||||
'name' => $this->getName(),
|
|
||||||
'message' => $this->getMessage(),
|
/**
|
||||||
'code' => $this->getCode(),
|
* Returns the array representation of the exception and all previous exceptions recursively.
|
||||||
);
|
* @param \Exception exception object
|
||||||
if (($prev = $this->getPrevious()) !== null) {
|
* @return array the array representation of the exception.
|
||||||
if ($prev instanceof self) {
|
*/
|
||||||
$array['previous'] = $prev->toArray();
|
protected function toArrayRecursive($exception)
|
||||||
} else {
|
{
|
||||||
$array['previous'] = array(
|
if ($exception instanceof self) {
|
||||||
'type' => get_class($prev),
|
$array = array(
|
||||||
'name' => 'Exception',
|
'type' => get_class($this),
|
||||||
'message' => $prev->getMessage(),
|
'name' => $this->getName(),
|
||||||
'code' => $prev->getCode(),
|
'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);
|
||||||
}
|
}
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user