10000 getWrappedExceptionsRecursively return array, deprecate other getExce… · symfony/symfony@fe56af8 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe56af8

Browse files
committed
getWrappedExceptionsRecursively return array, deprecate other getExceptions methods
1 parent a470754 commit fe56af8

6 files changed

+94
-52
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
1919
*/
20-
class DelayedMessageHandlingException extends RuntimeException implements NestedExceptionsInterface
20+
class DelayedMessageHandlingException extends RuntimeException implements WrappedExceptionsInterface
2121
{
2222
use WrappedExceptionsTrait;
2323

@@ -38,11 +38,16 @@ public function __construct(array $exceptions)
3838

3939
$this->exceptions = $exceptions;
4040

41-
parent::__construct($message, 0, $exceptions[0]);
41+
parent::__construct($message, 0, $exceptions[array_key_first($exceptions)]);
4242
}
4343

44+
/**
45+
* @deprecated since Symfony 6.4, use {@see self::getWrappedExceptions()} instead
46+
*/
4447
public function getExceptions(): array
4548
{
49+
trigger_deprecation('symfony/messenger', '6.4', 'The "%s()" method is deprecated, use "%s::getWrappedExceptions" instead.', __METHOD__, self::class);
50+
4651
return $this->exceptions;
4752
}
4853
}

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
use Symfony\Component\Messenger\Envelope;
1515

16-
class HandlerFailedException extends RuntimeException implements NestedExceptionsInterface
16+
class HandlerFailedException extends RuntimeException implements WrappedExceptionsInterface
1717
{
18-
use NestedExceptionsTrait;
18+
use WrappedExceptionsTrait;
1919

2020
private array $exceptions;
2121
private Envelope $envelope;
@@ -48,13 +48,20 @@ public function getEnvelope(): Envelope
4848
}
4949

5050
/**
51+
* @deprecated since Symfony 6.4, use {@see self::getWrappedExceptions()} instead
52+
*
5153
* @return \Throwable[]
5254
*/
5355
public function getNestedExceptions(): array
5456
{
57+
trigger_deprecation('symfony/messenger', '6.4', 'The "%s()" method is deprecated, use "%s::getWrappedExceptions" instead.', __METHOD__, self::class);
58+
5559
return $this->exceptions;
5660
}
5761

62+
/**
63+
* @deprecated since Symfony 6.4, use {@see self::getWrappedExceptions()} instead
64+
*/
5865
public function getNestedExceptionOfClass(string $exceptionClassName): array
5966
{
6067
trigger_deprecation('symfony/messenger', '6.4', 'The "%s()" method is deprecated, use "%s::getWrappedExceptions" instead.', __METHOD__, self::class);

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

-44
This file was deleted.

src/Symfony/Component/Messenger/Exception/NestedExceptionsInterface.php renamed to src/Symfony/Component/Messenger/Exception/WrappedExceptionsInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @author Jeroen <https://github.com/Jeroeny>
1818
*/
19-
interface NestedExceptionsInterface extends \Throwable
19+
interface WrappedExceptionsInterface extends \Throwable
2020
{
2121
/**
2222
* @return \Throwable[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 WrappedExceptionsTrait
18+
{
19+
/**
20+
* @return \Throwable[]
21+
*/
22+
public function getWrappedExceptions(string $class = null, bool $recursive = false): array
23+
{
24+
return $this->getWrappedExceptionsRecursively($class, $recursive, $this->exceptions);
25+
}
26+
27+
/**
28+
* @param class-string<\Throwable>|null $class
29+
* @param iterable<\Throwable> $exceptions
30+
*
31+
* @return \Throwable[]
32+
*/
33+
private function getWrappedExceptionsRecursively(?string $class, bool $recursive, iterable $exceptions): array
34+
{
35+
$unwrapped = [];
36+
foreach ($exceptions as $key => $exception) {
37+
if ($recursive && $exception instanceof WrappedExceptionsInterface) {
38+
$unwrapped[] = $this->getWrappedExceptionsRecursively($class, $recursive, $exception->getWrappedExceptions());
39+
40+
continue;
41+
}
42+
43+
if ($class && !is_a($exception, $class)) {
44+
continue;
45+
}
46+
47+
$unwrapped[] = [$key => $exception];
48+
}
49+
50+
return array_merge(...$unwrapped);
51+
}
52+
}

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

+25-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testThatNestedExceptionClassAreFound()
4747
$exception = new MyOwnException();
4848

4949
$handlerException = new HandlerFailedException($envelope, [new \LogicException(), $exception]);
50-
$this->assertSame([$exception], $handlerException->getNestedExceptionOfClass(MyOwnException::class));
50+
$this->assertSame([$exception], $handlerException->getWrappedExceptions(MyOwnException::class));
5151
}
5252

5353
public function testThatNestedExceptionClassAreFoundWhenUsingChildException()
@@ -56,7 +56,7 @@ public function testThatNestedExceptionClassAreFoundWhenUsingChildException()
5656
$exception = new MyOwnChildException();
5757

5858
$handlerException = new HandlerFailedException($envelope, [$exception]);
59-
$this->assertSame([$exception], $handlerException->getNestedExceptionOfClass(MyOwnException::class));
59+
$this->assertSame([$exception], $handlerException->getWrappedExceptions(MyOwnException::class));
6060
}
6161

6262
public function testThatNestedExceptionClassAreNotFoundIfNotPresent()
@@ -65,7 +65,7 @@ public function testThatNestedExceptionClassAreNotFoundIfNotPresent()
6565
$exception = new \LogicException();
6666

6767
$handlerException = new HandlerFailedException($envelope, [$exception]);
68-
$this->assertCount(0, $handlerException->getNestedExceptionOfClass(MyOwnException::class));
68+
$this->assertCount(0, $handlerException->getWrappedExceptions(MyOwnException::class));
6969
}
7070

7171
public function testThatWrappedExceptionsRecursive()
@@ -78,4 +78,26 @@ public function testThatWrappedExceptionsRecursive()
7878
$handlerException = new HandlerFailedException($envelope, [$exception1, $exception2, new DelayedMessageHandlingException([$exception3])]);
7979
$this->assertSame([$exception1, $exception2, $exception3], $handlerException->getWrappedExceptions(recursive: true));
8080
}
81+
82+
public function testThatWrappedExceptionsRecursiveStringKeys()
83+
{
84+
$envelope = new Envelope(new \stdClass());
85+
$exception1 = new \LogicException();
86+
$exception2 = new MyOwnException('second');
87+
$exception3 = new MyOwnException('third');
88+
89+
$handlerException = new HandlerFailedException($envelope, ['first' => $exception1, 'second' => $exception2, new DelayedMessageHandlingException(['third' => $exception3])]);
90+
$this->assertSame(['first' => $exception1, 'second' => $exception2, 'third' => $exception3], $handlerException->getWrappedExceptions(recursive: true));
91+
}
92+
93+
public function testThatWrappedExceptionsByClassRecursive()
94+
{
95+
$envelope = new Envelope(new \stdClass());
96+
$exception1 = new \LogicException();
97+
$exception2 = new MyOwnException('second');
98+
$exception3 = new MyOwnException('third');
99+
100+
$handlerException = new HandlerFailedException($envelope, [$exception1, $exception2, new DelayedMessageHandlingException([$exception3])]);
101+
$this->assertSame([$exception2, $exception3], $handlerException->getWrappedExceptions(class: MyOwnException::class, recursive: true));
102+
}
81103
}

0 commit comments

Comments
 (0)
0