HandlerFailedException.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. $message = sprintf('Handling "%s" failed: ', \get_class($envelope->getMessage()));
  23. parent::__construct(
  24. $message.(1 === \count($exceptions)
  25. ? $firstFailure->getMessage()
  26. : sprintf('%d handlers failed. First failure is: %s', \count($exceptions), $firstFailure->getMessage())
  27. ),
  28. (int) $firstFailure->getCode(),
  29. $firstFailure
  30. );
  31. $this->envelope = $envelope;
  32. $this->exceptions = $exceptions;
  33. }
  34. public function getEnvelope(): Envelope
  35. {
  36. return $this->envelope;
  37. }
  38. /**
  39. * @return \Throwable[]
  40. */
  41. public function getNestedExceptions(): array
  42. {
  43. return $this->exceptions;
  44. }
  45. public function getNestedExceptionOfClass(string $exceptionClassName): array
  46. {
  47. return array_values(
  48. array_filter(
  49. $this->exceptions,
  50. function ($exception) use ($exceptionClassName) {
  51. return is_a($exception, $exceptionClassName);
  52. }
  53. )
  54. );
  55. }
  56. }