[Notifier] Fix HttpClient TransportException handle

This commit is contained in:
Mathias Arlaud 2021-01-20 14:12:36 +01:00
parent 6b9fa0be67
commit bb32beb488
2 changed files with 21 additions and 4 deletions

View File

@ -15,6 +15,7 @@ use Symfony\Component\Mercure\PublisherInterface;
use Symfony\Component\Mercure\Update; use Symfony\Component\Mercure\Update;
use Symfony\Component\Notifier\Exception\InvalidArgumentException; use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\RuntimeException;
use Symfony\Component\Notifier\Exception\TransportException; use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Message\ChatMessage;
@ -23,6 +24,7 @@ use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport; use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
/** /**
@ -91,10 +93,12 @@ final class MercureTransport extends AbstractTransport
$sentMessage->setMessageId($messageId); $sentMessage->setMessageId($messageId);
return $sentMessage; return $sentMessage;
} catch (HttpExceptionInterface $e) {
throw new TransportException('Unable to post the Mercure message: '.$e->getResponse()->getContent(false), $e->getResponse(), $e->getCode(), $e);
} catch (ExceptionInterface $e) { } catch (ExceptionInterface $e) {
throw new TransportException(sprintf('Unable to post the Mercure message: "%s".', $e->getResponse()->getContent(false)), $e->getResponse(), $e->getCode(), $e); throw new RuntimeException('Unable to post the Mercure message: '.$e->getMessage(), $e->getCode(), $e);
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
throw new InvalidArgumentException(sprintf('Unable to post the Mercure message: "%s".', $e->getMessage()), $e->getCode(), $e); throw new InvalidArgumentException('Unable to post the Mercure message: '.$e->getMessage(), $e->getCode(), $e);
} }
} }
} }

View File

@ -11,12 +11,14 @@
namespace Symfony\Component\Notifier\Bridge\Mercure\Tests; namespace Symfony\Component\Notifier\Bridge\Mercure\Tests;
use Symfony\Component\HttpClient\Exception\TransportException as HttpClientTransportException;
use Symfony\Component\Mercure\PublisherInterface; use Symfony\Component\Mercure\PublisherInterface;
use Symfony\Component\Mercure\Update; use Symfony\Component\Mercure\Update;
use Symfony\Component\Notifier\Bridge\Mercure\MercureOptions; use Symfony\Component\Notifier\Bridge\Mercure\MercureOptions;
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransport; use Symfony\Component\Notifier\Bridge\Mercure\MercureTransport;
use Symfony\Component\Notifier\Exception\InvalidArgumentException; use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\RuntimeException;
use Symfony\Component\Notifier\Exception\TransportException; use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface; use Symfony\Component\Notifier\Message\MessageInterface;
@ -86,6 +88,17 @@ final class MercureTransportTest extends TransportTestCase
$this->createTransport()->send(new ChatMessage('testMessage', $this->createMock(MessageOptionsInterface::class))); $this->createTransport()->send(new ChatMessage('testMessage', $this->createMock(MessageOptionsInterface::class)));
} }
public function testSendWithTransportFailureThrows()
{
$publisher = $this->createMock(PublisherInterface::class);
$publisher->method('__invoke')->willThrowException(new HttpClientTransportException('Cannot connect to mercure'));
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unable to post the Mercure message: Cannot connect to mercure');
$this->createTransport(null, $publisher)->send(new ChatMessage('subject'));
}
public function testSendWithWrongResponseThrows() public function testSendWithWrongResponseThrows()
{ {
$response = $this->createMock(ResponseInterface::class); $response = $this->createMock(ResponseInterface::class);
@ -98,7 +111,7 @@ final class MercureTransportTest extends TransportTestCase
$publisher->method('__invoke')->willThrowException($httpException); $publisher->method('__invoke')->willThrowException($httpException);
$this->expectException(TransportException::class); $this->expectException(TransportException::class);
$this->expectExceptionMessage('Unable to post the Mercure message: "Service Unavailable".'); $this->expectExceptionMessage('Unable to post the Mercure message: Service Unavailable');
$this->createTransport(null, $publisher)->send(new ChatMessage('subject')); $this->createTransport(null, $publisher)->send(new ChatMessage('subject'));
} }
@ -109,7 +122,7 @@ final class MercureTransportTest extends TransportTestCase
$publisher->method('__invoke')->willThrowException(new \InvalidArgumentException('The provided JWT is not valid')); $publisher->method('__invoke')->willThrowException(new \InvalidArgumentException('The provided JWT is not valid'));
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unable to post the Mercure message: "The provided JWT is not valid".'); $this->expectExceptionMessage('Unable to post the Mercure message: The provided JWT is not valid');
$this->createTransport(null, $publisher)->send(new ChatMessage('subject')); $this->createTransport(null, $publisher)->send(new ChatMessage('subject'));
} }