Skip to content

Commit e7617da

Browse files
committed
feature #34704 [Messenger] Add method HandlerFailedException::getNestedExceptionOfClass (tyx)
This PR was merged into the 5.2-dev branch. Discussion ---------- [Messenger] Add method HandlerFailedException::getNestedExceptionOfClass | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | in progress I have many times use this kind of code on my own development. It helps when dealing with specific exception through a Messenger usage. Example in an ExceptionListener, the exception you get could be a `HandlerFailedException` but you want a specific treatment when the original exception is different (like a RedirectResponse, or Http error code different). edit: I finally also added a getNestedExceptionOfClass that could be useful too Commits ------- e0dd84b [Messenger] Add method HandlerFailedException::getNestedExceptionOfClass
2 parents a06e564 + e0dd84b commit e7617da

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,16 @@ public function getNestedExceptions(): array
4949
{
5050
return $this->exceptions;
5151
}
52+
53+
public function getNestedExceptionOfClass(string $exceptionClassName): array
54+
{
55+
return array_values(
56+
array_filter(
57+
$this->exceptions,
58+
function ($exception) use ($exceptionClassName) {
59+
return is_a($exception, $exceptionClassName);
60+
}
61+
)
62+
);
63+
}
5264
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\Messenger\Envelope;
77
use Symfony\Component\Messenger\Exception\HandlerFailedException;
8+
use Symfony\Component\Messenger\Tests\Fixtures\MyOwnChildException;
9+
use Symfony\Component\Messenger\Tests\Fixtures\MyOwnException;
810

911
class HandlerFailedExceptionTest extends TestCase
1012
{
@@ -28,4 +30,31 @@ public function __construct()
2830
$this->assertIsString($originalException->getCode(), 'Original exception code still with original type (string)');
2931
$this->assertSame($exception->getCode(), $originalException->getCode(), 'Original exception code is not modified');
3032
}
33+
34+
public function testThatNestedExceptionClassAreFound()
35+
{
36+
$envelope = new Envelope(new \stdClass());
37+
$exception = new MyOwnException();
38+
39+
$handlerException = new HandlerFailedException($envelope, [new \LogicException(), $exception]);
40+
$this->assertSame([$exception], $handlerException->getNestedExceptionOfClass(MyOwnException::class));
41+
}
42+
43+
public function testThatNestedExceptionClassAreFoundWhenUsingChildException()
44+
{
45+
$envelope = new Envelope(new \stdClass());
46+
$exception = new MyOwnChildException();
47+
48+
$handlerException = new HandlerFailedException($envelope, [$exception]);
49+
$this->assertSame([$exception], $handlerException->getNestedExceptionOfClass(MyOwnException::class));
50+
}
51+
52+
public function testThatNestedExceptionClassAreNotFoundIfNotPresent()
53+
{
54+
$envelope = new Envelope(new \stdClass());
55+
$exception = new \LogicException();
56+
57+
$handlerException = new HandlerFailedException($envelope, [$exception]);
58+
$this->assertCount(0, $handlerException->getNestedExceptionOfClass(MyOwnException::class));
59+
}
3160
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Tests\Fixtures;
13+
14+
class MyOwnChildException extends MyOwnException
15+
{
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Tests\Fixtures;
13+
14+
class MyOwnException extends \Exception
15+
{
16+
}

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