HandlerFailedException.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\Messenger\Exception;
  11. use Symfony\Component\Messenger\Envelope;
  12. class HandlerFailedException extends RuntimeException
  13. {
  14. private $exceptions;
  15. private $envelope;
  16. /**
  17. * @param \Throwable[] $exceptions
  18. */
  19. public function __construct(Envelope $envelope, array $exceptions)
  20. {
  21. $firstFailure = current($exceptions);
  22. parent::__construct(
  23. 1 === \count($exceptions)
  24. ? $firstFailure->getMessage()
  25. : sprintf('%d handlers failed. First failure is: "%s"', \count($exceptions), $firstFailure->getMessage()),
  26. (int) $firstFailure->getCode(),
  27. $firstFailure
  28. );
  29. $this->envelope = $envelope;
  30. $this->exceptions = $exceptions;
  31. }
  32. public function getEnvelope(): Envelope
  33. {
  34. return $this->envelope;
  35. }
  36. /**
  37. * @return \Throwable[]
  38. */
  39. public function getNestedExceptions(): array
  40. {
  41. return $this->exceptions;
  42. }
  43. public function getNestedExceptionOfClass(string $exceptionClassName): array
  44. {
  45. return array_values(
  46. array_filter(
  47. $this->exceptions,
  48. function ($exception) use ($exceptionClassName) {
  49. return is_a($exception, $exceptionClassName);
  50. }
  51. )
  52. );
  53. }
  54. }