Browse Source

Recursively return previous exceptions in toArray().

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
b184a75d68
  1. 24
      framework/yii/base/Exception.php

24
framework/yii/base/Exception.php

@ -29,23 +29,33 @@ class Exception extends \Exception implements Arrayable
*/ */
public function toArray() public function toArray()
{ {
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( $array = array(
'type' => get_class($this), 'type' => get_class($this),
'name' => $this->getName(), 'name' => $this->getName(),
'message' => $this->getMessage(), 'message' => $this->getMessage(),
'code' => $this->getCode(), 'code' => $this->getCode(),
); );
if (($prev = $this->getPrevious()) !== null) {
if ($prev instanceof self) {
$array['previous'] = $prev->toArray();
} else { } else {
$array['previous'] = array( $array = array(
'type' => get_class($prev), 'type' => get_class($exception),
'name' => 'Exception', 'name' => 'Exception',
'message' => $prev->getMessage(), 'message' => $exception->getMessage(),
'code' => $prev->getCode(), 'code' => $exception->getCode(),
); );
} }
if (($prev = $exception->getPrevious()) !== null) {
$array['previous'] = $this->toArrayRecursive($prev);
} }
return $array; return $array;
} }

Loading…
Cancel
Save