8000 Add `NestedExceptionsInterface` interface for exceptions that hold mu… · symfony/symfony@faf2883 · GitHub
[go: up one dir, main page]

Skip to content

Commit faf2883

Browse files
committed
Add NestedExceptionsInterface interface for exceptions that hold multiple individual exceptions
1 parent 7c833ee commit faf2883

7 files changed

+89
-10
lines changed

UPGRADE-6.4.md

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Messenger
112112
---------
113113

114114
* Deprecate `StopWorkerOnSignalsListener` in favor of using the `SignalableCommandInterface`
115+
* Deprecate `DelayedMessageHandlingException::getExceptions` which is replaced by `DelayedMessageHandlingException::getNestedExceptions`
115116

116117
MonologBridge
117118
-------------

src/Symfony/Component/Messenger/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Deprecate `StopWorkerOnSignalsListener` in favor of using the `SignalableCommandInterface`
88
* Add `HandlerDescriptor::getOptions`
9+
* Add `NestedExceptionsInterface` interface for exceptions that hold multiple individual exceptions
910

1011
6.3
1112
---

src/Symfony/Component/Messenger/Exception/DelayedMessageHandlingException.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
*
1818
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
1919
*/
20-
class DelayedMessageHandlingException extends RuntimeException
20+
class DelayedMessageHandlingException extends RuntimeException implements NestedExceptionsInterface
2121
{
22+
use NestedExceptionsTrait;
23+
2224
private array $exceptions;
2325

2426
public function __construct(array $exceptions)
@@ -39,8 +41,13 @@ public function __construct(array $exceptions)
3941
parent::__construct($message, 0, $exceptions[0]);
4042
}
4143

44+
/**
45+
* @deprecated since Symfony 6.4, use {@link self::getNestedExceptions()} instead
46+
*/
4247
public function getExceptions(): array
4348
{
49+
trigger_deprecation('symfony/messenger', '6.4', 'The "%s()" method is deprecated, use "%s::getNestedExceptions" instead.', __METHOD__, self::class);
50+
4451
return $this->exceptions;
4552
}
4653
}

src/Symfony/Component/Messenger/Exception/HandlerFailedException.php

+3-9
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
use Symfony\Component\Messenger\Envelope;
1515

16-
class HandlerFailedException extends RuntimeException
16+
class HandlerFailedException extends RuntimeException implements NestedExceptionsInterface
1717
{
18+
use NestedExceptionsTrait;
19+
1820
private array $exceptions;
1921
private Envelope $envelope;
2022

@@ -45,14 +47,6 @@ public function getEnvelope(): Envelope
4547
return $this->envelope;
4648
}
4749

48-
/**
49-
* @return \Throwable[]
50-
*/
51-
public function getNestedExceptions(): array
52-
{
53-
return $this->exceptions;
54-
}
55-
5650
public function getNestedExceptionOfClass(string $exceptionClassName): array
5751
{
5852
return array_values(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Exception;
13+
14+
/**
15+
* Exception that holds multiple exceptions thrown by one or more handlers and/or messages.
16+
*
17+
* @author Jeroen <https://github.com/Jeroeny>
18+
*/
19+
interface NestedExceptionsInterface extends \Throwable
20+
{
21+
/**
22+
* @return \Throwable[]
23+
*/
24+
public function getNestedExceptions(bool $recursive = true): array;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Exception;
13+
14+
/**
15+
* @author Jeroen <https://github.com/Jeroeny>
16+
*/
17+
trait NestedExceptionsTrait
18+
{
19+
/**
20+
* @return \Throwable[]
21+
*/
22+
public function getNestedExceptions(bool $recursive = true): array
23+
{
24+
return $recursive ? iterator_to_array($this->getNestedExceptionsRecursively($this->exceptions), false) : $this->exceptions;
25+
}
26+
27+
private function getNestedExceptionsRecursively(iterable $exceptions): \Traversable
28+
{
29+
foreach ($exceptions as $exception) {
30+
if ($exception instanceof NestedExceptionsInterface) {
31+
yield from $this->getNestedExceptionsRecursively($exception->getNestedExceptions(false));
32+
33+
return;
34+
}
35+
36+
yield $exception;
37+
}
38+
}
39+
}

src/Symfony/Component/Messenger/Tests/Exception/HandlerFailedExceptionTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Messenger\Envelope;
16+
use Symfony\Component\Messenger\Exception\DelayedMessageHandlingException;
1617
use Symfony\Component\Messenger\Exception\HandlerFailedException;
1718
use Symfony\Component\Messenger\Tests\Fixtures\MyOwnChildException;
1819
use Symfony\Component\Messenger\Tests\Fixtures\MyOwnException;
@@ -66,4 +67,15 @@ public function testThatNestedExceptionClassAreNotFoundIfNotPresent()
6667
$handlerException = new HandlerFailedException($envelope, [$exception]);
6768
$this->assertCount(0, $handlerException->getNestedExceptionOfClass(MyOwnException::class));
6869
}
70+
71+
public function testThatNestedExceptionsRecursive()
72+
{
73+
$envelope = new Envelope(new \stdClass());
74+
$exception1 = new \LogicException();
75+
$exception2 = new MyOwnException('second');
76+
$exception3 = new MyOwnException('third');
77+
78+
$handlerException = new HandlerFailedException($envelope, [$exception1, $exception2, new DelayedMessageHandlingException([$exception3])]);
79+
$this->assertSame([$exception1, $exception2, $exception3], $handlerException->getNestedExceptions(true));
80+
}
6981
}

0 commit comments

Comments
 (0)
0