ExceptionDataCollector.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\DataCollector;
  11. use Symfony\Component\Debug\Exception\FlattenException;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. /**
  15. * ExceptionDataCollector.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class ExceptionDataCollector extends DataCollector
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function collect(Request $request, Response $response, \Exception $exception = null)
  25. {
  26. if (null !== $exception) {
  27. $this->data = array(
  28. 'exception' => FlattenException::create($exception),
  29. );
  30. }
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function reset()
  36. {
  37. $this->data = array();
  38. }
  39. /**
  40. * Checks if the exception is not null.
  41. *
  42. * @return bool true if the exception is not null, false otherwise
  43. */
  44. public function hasException()
  45. {
  46. return isset($this->data['exception']);
  47. }
  48. /**
  49. * Gets the exception.
  50. *
  51. * @return \Exception The exception
  52. */
  53. public function getException()
  54. {
  55. return $this->data['exception'];
  56. }
  57. /**
  58. * Gets the exception message.
  59. *
  60. * @return string The exception message
  61. */
  62. public function getMessage()
  63. {
  64. return $this->data['exception']->getMessage();
  65. }
  66. /**
  67. * Gets the exception code.
  68. *
  69. * @return int The exception code
  70. */
  71. public function getCode()
  72. {
  73. return $this->data['exception']->getCode();
  74. }
  75. /**
  76. * Gets the status code.
  77. *
  78. * @return int The status code
  79. */
  80. public function getStatusCode()
  81. {
  82. return $this->data['exception']->getStatusCode();
  83. }
  84. /**
  85. * Gets the exception trace.
  86. *
  87. * @return array The exception trace
  88. */
  89. public function getTrace()
  90. {
  91. return $this->data['exception']->getTrace();
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function getName()
  97. {
  98. return 'exception';
  99. }
  100. }