From 3bebc645bc654740cf5ece0829af10a302a03da8 Mon Sep 17 00:00:00 2001 From: Paul Oms Date: Mon, 6 Jun 2022 16:49:04 +0100 Subject: [PATCH] [Mailer] Fix Error Handling for OhMySMTP Bridge --- .../Transport/OhMySmtpApiTransportTest.php | 28 +++++++++++++++++-- .../Transport/OhMySmtpApiTransport.php | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Mailer/Bridge/OhMySmtp/Tests/Transport/OhMySmtpApiTransportTest.php b/src/Symfony/Component/Mailer/Bridge/OhMySmtp/Tests/Transport/OhMySmtpApiTransportTest.php index 7f64a8c997fdb..6fc5c5ba9d215 100644 --- a/src/Symfony/Component/Mailer/Bridge/OhMySmtp/Tests/Transport/OhMySmtpApiTransportTest.php +++ b/src/Symfony/Component/Mailer/Bridge/OhMySmtp/Tests/Transport/OhMySmtpApiTransportTest.php @@ -101,7 +101,7 @@ public function testSend() public function testSendThrowsForErrorResponse() { $client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface { - return new MockResponse(json_encode(['Message' => 'i\'m a teapot', 'ErrorCode' => 418]), [ + return new MockResponse(json_encode(['error' => 'i\'m a teapot']), [ 'http_code' => 418, 'response_headers' => [ 'content-type' => 'application/json', @@ -118,7 +118,31 @@ public function testSendThrowsForErrorResponse() ->text('Hello There!'); $this->expectException(HttpTransportException::class); - $this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).'); + $this->expectExceptionMessage('Unable to send an email: {"error":"i\'m a teapot"}'); + $transport->send($mail); + } + + public function testSendThrowsForMultipleErrorResponses() + { + $client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface { + return new MockResponse(json_encode(['errors' => ["to" => "undefined field" ]]), [ + 'http_code' => 418, + 'response_headers' => [ + 'content-type' => 'application/json', + ], + ]); + }); + $transport = new OhMySmtpApiTransport('KEY', $client); + $transport->setPort(8984); + + $mail = new Email(); + $mail->subject('Hello!') + ->to(new Address('saif.gmati@symfony.com', 'Saif Eddin')) + ->from(new Address('fabpot@symfony.com', 'Fabien')) + ->text('Hello There!'); + + $this->expectException(HttpTransportException::class); + $this->expectExceptionMessage('Unable to send an email: {"errors":{"to":"undefined field"}}'); $transport->send($mail); } diff --git a/src/Symfony/Component/Mailer/Bridge/OhMySmtp/Transport/OhMySmtpApiTransport.php b/src/Symfony/Component/Mailer/Bridge/OhMySmtp/Transport/OhMySmtpApiTransport.php index a70fc3448e1c2..e4e6bddfc103d 100644 --- a/src/Symfony/Component/Mailer/Bridge/OhMySmtp/Transport/OhMySmtpApiTransport.php +++ b/src/Symfony/Component/Mailer/Bridge/OhMySmtp/Transport/OhMySmtpApiTransport.php @@ -67,7 +67,7 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e } if (200 !== $statusCode) { - throw new HttpTransportException('Unable to send an email: '.$result['Message'].sprintf(' (code %d).', $result['ErrorCode']), $response); + throw new HttpTransportException('Unable to send an email: '.$response->getContent(false), $response); } $sentMessage->setMessageId($result['id']);