Rename exception, add change log and a few other things

This commit is contained in:
Samuel ROZE 2019-04-06 12:33:50 +02:00
parent e6e4cde5fc
commit 2e5e910229
8 changed files with 63 additions and 73 deletions

View File

@ -79,6 +79,7 @@ CHANGELOG
* Added a `SetupTransportsCommand` command to setup the transports
* Added a Doctrine transport. For example, use the `doctrine://default` DSN (this uses the `default` Doctrine entity manager)
* [BC BREAK] The `getConnectionConfiguration` method on Amqp's `Connection` has been removed.
* [BC BREAK] A `HandlerFailedException` exception will be thrown if one or more handler fails.
4.2.0
-----

View File

@ -1,61 +0,0 @@
<?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.
*/
declare(strict_types=1);
namespace Symfony\Component\Messenger\Exception;
use Symfony\Component\Messenger\Envelope;
class ChainedHandlerFailedException extends \RuntimeException implements ExceptionInterface
{
/**
* @var \Throwable[]
*/
private $nested;
/**
* @var Envelope
*/
private $envelope;
public function __construct(Envelope $envelope, \Throwable ...$nested)
{
parent::__construct($this->constructMessage($nested));
$this->envelope = $envelope;
$this->nested = $nested;
}
public function getEnvelope(): Envelope
{
return $this->envelope;
}
/**
* @return \Throwable[]
*/
public function getNestedExceptions(): array
{
return $this->nested;
}
/**
* @param \Throwable[] $nested
*
* @return string
*/
private function constructMessage(array $nested): string
{
return 1 === \count($nested) ?
$nested[0]->getMessage() :
sprintf('%d MessageHandler failed. First one failed with Message: %s', \count($nested), $nested[0]->getMessage());
}
}

View File

@ -0,0 +1,52 @@
<?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\Exception;
use Symfony\Component\Messenger\Envelope;
class HandlerFailedException extends RuntimeException
{
private $exceptions;
private $envelope;
/**
* @param \Throwable[] $exceptions
*/
public function __construct(Envelope $envelope, array $exceptions)
{
$firstFailure = current($exceptions);
parent::__construct(
1 === \count($exceptions)
? $firstFailure->getMessage()
: sprintf('%d handlers failed. First failure is: "%s"', \count($exceptions), $firstFailure->getMessage()),
$firstFailure->getCode(),
$firstFailure
);
$this->envelope = $envelope;
$this->exceptions = $exceptions;
}
public function getEnvelope(): Envelope
{
return $this->envelope;
}
/**
* @return \Throwable[]
*/
public function getNestedExceptions(): array
{
return $this->exceptions;
}
}

View File

@ -14,7 +14,7 @@ namespace Symfony\Component\Messenger\Middleware;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\ChainedHandlerFailedException;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;
use Symfony\Component\Messenger\Handler\HandlersLocatorInterface;
use Symfony\Component\Messenger\Stamp\HandledStamp;
@ -79,7 +79,7 @@ class HandleMessageMiddleware implements MiddlewareInterface
}
if (\count($exceptions)) {
throw new ChainedHandlerFailedException($envelope, ...$exceptions);
throw new HandlerFailedException($envelope, $exceptions);
}
return $stack->next()->handle($envelope, $stack);

View File

@ -11,7 +11,7 @@
namespace Symfony\Component\Messenger\Tests\Fixtures;
class MessageHandlerFailingFirstTimes
class DummyMessageHandlerFailingFirstTimes
{
private $remainingFailures;

View File

@ -12,7 +12,7 @@
namespace Symfony\Component\Messenger\Tests\Middleware;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\ChainedHandlerFailedException;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Handler\HandlersLocator;
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
use Symfony\Component\Messenger\Middleware\StackMiddleware;
@ -52,7 +52,7 @@ class HandleMessageMiddlewareTest extends MiddlewareTestCase
try {
$envelope = $middleware->handle($envelope, $this->getStackMock($nextIsCalled));
} catch (ChainedHandlerFailedException $e) {
} catch (HandlerFailedException $e) {
$envelope = $e->getEnvelope();
}

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* This file is part of the Symfony package.
*
@ -22,7 +20,7 @@ use Symfony\Component\Messenger\Middleware\SendMessageMiddleware;
use Symfony\Component\Messenger\Retry\MultiplierRetryStrategy;
use Symfony\Component\Messenger\Stamp\SentStamp;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\MessageHandlerFailingFirstTimes;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageHandlerFailingFirstTimes;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Sender\SendersLocator;
use Symfony\Component\Messenger\Worker;
@ -43,8 +41,8 @@ class RetryIntegrationTest extends TestCase
$senderLocator = new SendersLocator([], ['*' => true]);
$handler = new MessageHandlerFailingFirstTimes();
$throwingHandler = new MessageHandlerFailingFirstTimes(1);
$handler = new DummyMessageHandlerFailingFirstTimes();
$throwingHandler = new DummyMessageHandlerFailingFirstTimes(1);
$handlerLocator = new HandlersLocator([
DummyMessage::class => [
'handler' => $handler,

View File

@ -15,7 +15,7 @@ use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
use Symfony\Component\Messenger\Exception\ChainedHandlerFailedException;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\Retry\RetryStrategyInterface;
@ -124,7 +124,7 @@ class Worker implements WorkerInterface
try {
$envelope = $this->bus->dispatch($envelope->with(new ReceivedStamp()));
} catch (\Throwable $throwable) {
if ($throwable instanceof ChainedHandlerFailedException) {
if ($throwable instanceof HandlerFailedException) {
$envelope = $throwable->getEnvelope();
}