-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Messenger] Added FailedMessageErrorDetailsStamp
- Loading branch information
Showing
13 changed files
with
383 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/Messenger/EventListener/AddErrorDetailsStampListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; | ||
use Symfony\Component\Messenger\Stamp\ErrorDetailsStamp; | ||
|
||
final class AddErrorDetailsStampListener implements EventSubscriberInterface | ||
{ | ||
public function onMessageFailed(WorkerMessageFailedEvent $event): void | ||
{ | ||
/** @var ErrorDetailsStamp|null $previousStamp */ | ||
$previousStamp = $event->getEnvelope()->last(ErrorDetailsStamp::class); | ||
$stamp = new ErrorDetailsStamp($event->getThrowable()); | ||
|
||
// Do not append duplicate information | ||
if (null === $previousStamp || !$previousStamp->equals($stamp)) { | ||
$event->addStamps($stamp); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
// must have higher priority than SendFailedMessageForRetryListener | ||
WorkerMessageFailedEvent::class => ['onMessageFailed', 200], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/Symfony/Component/Messenger/Stamp/ErrorDetailsStamp.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Stamp; | ||
|
||
use Symfony\Component\ErrorHandler\Exception\FlattenException; | ||
use Symfony\Component\Messenger\Exception\HandlerFailedException; | ||
use Throwable; | ||
|
||
/** | ||
* Stamp applied when a messages fails due to an exception in the handler. | ||
*/ | ||
final class ErrorDetailsStamp implements StampInterface | ||
{ | ||
/** @var string */ | ||
private $exceptionClass; | ||
|
||
/** @var int|mixed */ | ||
private $exceptionCode; | ||
|
||
/** @var string */ | ||
private $exceptionMessage; | ||
|
||
/** @var FlattenException|null */ | ||
private $flattenException; | ||
|
||
public function __construct(Throwable $throwable) | ||
{ | ||
if ($throwable instanceof HandlerFailedException) { | ||
$throwable = $throwable->getPrevious(); | ||
} | ||
|
||
$this->exceptionClass = \get_class($throwable); | ||
$this->exceptionCode = $throwable->getCode(); | ||
$this->exceptionMessage = $throwable->getMessage(); | ||
|
||
if (class_exists(FlattenException::class)) { | ||
$this->flattenException = FlattenException::createFromThrowable($throwable); | ||
} | ||
} | ||
|
||
public function getExceptionClass(): string | ||
{ | ||
return $this->exceptionClass; | ||
} | ||
|
||
public function getExceptionCode() | ||
{ | ||
return $this->exceptionCode; | ||
} | ||
|
||
public function getExceptionMessage(): string | ||
{ | ||
return $this->exceptionMessage; | ||
} | ||
|
||
public function getFlattenException(): ?FlattenException | ||
{ | ||
return $this->flattenException; | ||
} | ||
|
||
public function equals(self $that): bool | ||
{ | ||
if ($this->flattenException && $that->flattenException) { | ||
return $this->flattenException->getClass() === $that->flattenException->getClass() | ||
&& $this->flattenException->getCode() === $that->flattenException->getCode() | ||
&& $this->flattenException->getMessage() === $that->flattenException->getMessage(); | ||
} | ||
|
||
return $this->exceptionClass === $that->exceptionClass | ||
&& $this->exceptionCode === $that->exceptionCode | ||
&& $this->exceptionMessage === $that->exceptionMessage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.