From b184a75d6871a03e12faeb17a5dc44152fe4b18b Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 14 Jun 2013 17:15:28 -0400 Subject: [PATCH] Recursively return previous exceptions in toArray(). --- framework/yii/base/Exception.php | 44 ++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/framework/yii/base/Exception.php b/framework/yii/base/Exception.php index 19317ca..4f66e53 100644 --- a/framework/yii/base/Exception.php +++ b/framework/yii/base/Exception.php @@ -29,23 +29,33 @@ class Exception extends \Exception implements Arrayable */ public function toArray() { - $array = array( - 'type' => get_class($this), - 'name' => $this->getName(), - 'message' => $this->getMessage(), - 'code' => $this->getCode(), - ); - if (($prev = $this->getPrevious()) !== null) { - if ($prev instanceof self) { - $array['previous'] = $prev->toArray(); - } else { - $array['previous'] = array( - 'type' => get_class($prev), - 'name' => 'Exception', - 'message' => $prev->getMessage(), - 'code' => $prev->getCode(), - ); - } + 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); } return $array; }