Exception.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\base;
  8. /**
  9. * Exception represents a generic exception for all purposes.
  10. *
  11. * @author Qiang Xue <[email protected]>
  12. * @since 2.0
  13. */
  14. class Exception extends \Exception implements Arrayable
  15. {
  16. /**
  17. * @return string the user-friendly name of this exception
  18. */
  19. public function getName()
  20. {
  21. return 'Exception';
  22. }
  23. /**
  24. * Returns the array representation of this object.
  25. * @return array the array representation of this object.
  26. */
  27. public function toArray()
  28. {
  29. return $this->toArrayRecursive($this);
  30. }
  31. /**
  32. * Returns the array representation of the exception and all previous exceptions recursively.
  33. * @param \Exception $exception object
  34. * @return array the array representation of the exception.
  35. */
  36. protected function toArrayRecursive($exception)
  37. {
  38. $array = [
  39. 'type' => get_class($exception),
  40. 'name' => $exception instanceof self ? $exception->getName() : 'Exception',
  41. 'message' => $exception->getMessage(),
  42. 'code' => $exception->getCode(),
  43. ];
  44. if (($prev = $exception->getPrevious()) !== null) {
  45. $array['previous'] = $this->toArrayRecursive($prev);
  46. }
  47. return $array;
  48. }
  49. }