Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfe56af8

Browse files
committed
getWrappedExceptionsRecursively return array, deprecate other getExceptions methods
1 parenta470754 commitfe56af8

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 DelayedMessageHandlingExceptionextends RuntimeExceptionimplementsNestedExceptionsInterface
20+
class DelayedMessageHandlingExceptionextends RuntimeExceptionimplementsWrappedExceptionsInterface
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
publicfunctiongetExceptions():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
useSymfony\Component\Messenger\Envelope;
1515

16-
class HandlerFailedExceptionextends RuntimeExceptionimplementsNestedExceptionsInterface
16+
class HandlerFailedExceptionextends RuntimeExceptionimplementsWrappedExceptionsInterface
1717
{
18-
useNestedExceptionsTrait;
18+
useWrappedExceptionsTrait;
1919

2020
privatearray$exceptions;
2121
privateEnvelope$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
publicfunctiongetNestedExceptions():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
publicfunctiongetNestedExceptionOfClass(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-
interfaceNestedExceptionsInterfaceextends \Throwable
19+
interfaceWrappedExceptionsInterfaceextends \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+
namespaceSymfony\Component\Messenger\Exception;
13+
14+
/**
15+
* @author Jeroen <https://github.com/Jeroeny>
16+
*/
17+
trait WrappedExceptionsTrait
18+
{
19+
/**
20+
* @return \Throwable[]
21+
*/
22+
publicfunctiongetWrappedExceptions(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+
privatefunctiongetWrappedExceptionsRecursively(?string$class,bool$recursive,iterable$exceptions):array
34+
{
35+
$unwrapped = [];
36+
foreach ($exceptionsas$key =>$exception) {
37+
if ($recursive &&$exceptioninstanceof 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+
returnarray_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 =newMyOwnException();
4848

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

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

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

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

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

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp