No Description

ExceptionDataCollector.php 2.4KB

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