ExceptionDataCollector.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
  22. {
  23. if (null !== $exception) {
  24. $this->data = [
  25. 'exception' => FlattenException::createWithDataRepresentation($exception),
  26. ];
  27. }
  28. }
  29. public function hasException(): bool
  30. {
  31. return isset($this->data['exception']);
  32. }
  33. public function getException(): \Exception|FlattenException
  34. {
  35. return $this->data['exception'];
  36. }
  37. public function getMessage(): string
  38. {
  39. return $this->data['exception']->getMessage();
  40. }
  41. public function getCode(): int
  42. {
  43. return $this->data['exception']->getCode();
  44. }
  45. public function getStatusCode(): int
  46. {
  47. return $this->data['exception']->getStatusCode();
  48. }
  49. public function getTrace(): array
  50. {
  51. return $this->data['exception']->getTrace();
  52. }
  53. public function getName(): string
  54. {
  55. return 'exception';
  56. }
  57. }