Skip to content

Commit fe56af8

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

File tree

6 files changed

+94
-52
lines changed

6 files changed

+94
-52
lines changed

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

Lines changed: 7 additions & 2 deletions
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

Lines changed: 9 additions & 2 deletions
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

Lines changed: 0 additions & 44 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
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[]
Lines changed: 52 additions & 0 deletions
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

Lines changed: 25 additions & 3 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy