You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.3 KiB

3 years ago
  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\ErrorHandler\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. * @final
  20. */
  21. class ExceptionDataCollector extends DataCollector
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function collect(Request $request, Response $response, \Throwable $exception = null)
  27. {
  28. if (null !== $exception) {
  29. $this->data = [
  30. 'exception' => FlattenException::createFromThrowable($exception),
  31. ];
  32. }
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function reset()
  38. {
  39. $this->data = [];
  40. }
  41. /**
  42. * Checks if the exception is not null.
  43. *
  44. * @return bool true if the exception is not null, false otherwise
  45. */
  46. public function hasException()
  47. {
  48. return isset($this->data['exception']);
  49. }
  50. /**
  51. * Gets the exception.
  52. *
  53. * @return \Exception|FlattenException
  54. */
  55. public function getException()
  56. {
  57. return $this->data['exception'];
  58. }
  59. /**
  60. * Gets the exception message.
  61. *
  62. * @return string The exception message
  63. */
  64. public function getMessage()
  65. {
  66. return $this->data['exception']->getMessage();
  67. }
  68. /**
  69. * Gets the exception code.
  70. *
  71. * @return int The exception code
  72. */
  73. public function getCode()
  74. {
  75. return $this->data['exception']->getCode();
  76. }
  77. /**
  78. * Gets the status code.
  79. *
  80. * @return int The status code
  81. */
  82. public function getStatusCode()
  83. {
  84. return $this->data['exception']->getStatusCode();
  85. }
  86. /**
  87. * Gets the exception trace.
  88. *
  89. * @return array The exception trace
  90. */
  91. public function getTrace()
  92. {
  93. return $this->data['exception']->getTrace();
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function getName()
  99. {
  100. return 'exception';
  101. }
  102. }