ExceptionDataCollector.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @final
  18. */
  19. class ExceptionDataCollector extends DataCollector
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function collect(Request $request, Response $response, \Throwable $exception = null)
  25. {
  26. if (null !== $exception) {
  27. $this->data = [
  28. 'exception' => FlattenException::createFromThrowable($exception),
  29. ];
  30. }
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function reset()
  36. {
  37. $this->data = [];
  38. }
  39. public function hasException(): bool
  40. {
  41. return isset($this->data['exception']);
  42. }
  43. public function getException(): \Exception|FlattenException
  44. {
  45. return $this->data['exception'];
  46. }
  47. public function getMessage(): string
  48. {
  49. return $this->data['exception']->getMessage();
  50. }
  51. public function getCode(): int
  52. {
  53. return $this->data['exception']->getCode();
  54. }
  55. public function getStatusCode(): int
  56. {
  57. return $this->data['exception']->getStatusCode();
  58. }
  59. public function getTrace(): array
  60. {
  61. return $this->data['exception']->getTrace();
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getName(): string
  67. {
  68. return 'exception';
  69. }
  70. }