From 2deef0aee4e69571eb19353e2600af5e51ebdfe5 Mon Sep 17 00:00:00 2001 From: Bastien Chalier Date: Thu, 5 Oct 2023 16:52:19 +0200 Subject: [PATCH] throw TransportException instead of Exception in SES and SES v2 mail driver --- .../Mail/Transport/SesTransport.php | 4 ++-- .../Mail/Transport/SesV2Transport.php | 4 ++-- tests/Mail/MailSesTransportTest.php | 20 +++++++++++++++++++ tests/Mail/MailSesV2TransportTest.php | 20 +++++++++++++++++++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Mail/Transport/SesTransport.php b/src/Illuminate/Mail/Transport/SesTransport.php index 9db7734c62ad..443ad934e031 100644 --- a/src/Illuminate/Mail/Transport/SesTransport.php +++ b/src/Illuminate/Mail/Transport/SesTransport.php @@ -4,7 +4,7 @@ use Aws\Exception\AwsException; use Aws\Ses\SesClient; -use Exception; +use Symfony\Component\Mailer\Exception\TransportException; use Symfony\Component\Mailer\Header\MetadataHeader; use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mailer\Transport\AbstractTransport; @@ -75,7 +75,7 @@ protected function doSend(SentMessage $message): void } catch (AwsException $e) { $reason = $e->getAwsErrorMessage() ?? $e->getMessage(); - throw new Exception( + throw new TransportException( sprintf('Request to AWS SES API failed. Reason: %s.', $reason), is_int($e->getCode()) ? $e->getCode() : 0, $e diff --git a/src/Illuminate/Mail/Transport/SesV2Transport.php b/src/Illuminate/Mail/Transport/SesV2Transport.php index 5cc3936d85b6..876630b9e1be 100644 --- a/src/Illuminate/Mail/Transport/SesV2Transport.php +++ b/src/Illuminate/Mail/Transport/SesV2Transport.php @@ -4,7 +4,7 @@ use Aws\Exception\AwsException; use Aws\SesV2\SesV2Client; -use Exception; +use Symfony\Component\Mailer\Exception\TransportException; use Symfony\Component\Mailer\Header\MetadataHeader; use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mailer\Transport\AbstractTransport; @@ -79,7 +79,7 @@ protected function doSend(SentMessage $message): void } catch (AwsException $e) { $reason = $e->getAwsErrorMessage() ?? $e->getMessage(); - throw new Exception( + throw new TransportException( sprintf('Request to AWS SES V2 API failed. Reason: %s.', $reason), is_int($e->getCode()) ? $e->getCode() : 0, $e diff --git a/tests/Mail/MailSesTransportTest.php b/tests/Mail/MailSesTransportTest.php index f21b7256474f..8d3fd4c6b83d 100755 --- a/tests/Mail/MailSesTransportTest.php +++ b/tests/Mail/MailSesTransportTest.php @@ -2,6 +2,8 @@ namespace Illuminate\Tests\Mail; +use Aws\Command; +use Aws\Exception\AwsException; use Aws\Ses\SesClient; use Illuminate\Config\Repository; use Illuminate\Container\Container; @@ -10,6 +12,7 @@ use Illuminate\View\Factory; use Mockery as m; use PHPUnit\Framework\TestCase; +use Symfony\Component\Mailer\Exception\TransportException; use Symfony\Component\Mailer\Header\MetadataHeader; use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Email; @@ -78,6 +81,23 @@ public function testSend() (new SesTransport($client))->send($message); } + public function testSendError() + { + $message = new Email(); + $message->subject('Foo subject'); + $message->text('Bar body'); + $message->sender('myself@example.com'); + $message->to('me@example.com'); + + $client = m::mock(SesClient::class); + $client->shouldReceive('sendRawEmail')->once() + ->andThrow(new AwsException('Email address is not verified.', new Command('sendRawEmail'))); + + $this->expectException(TransportException::class); + + (new SesTransport($client))->send($message); + } + public function testSesLocalConfiguration() { $container = new Container; diff --git a/tests/Mail/MailSesV2TransportTest.php b/tests/Mail/MailSesV2TransportTest.php index c95a01440ecf..7b7821558ac7 100755 --- a/tests/Mail/MailSesV2TransportTest.php +++ b/tests/Mail/MailSesV2TransportTest.php @@ -2,6 +2,8 @@ namespace Illuminate\Tests\Mail; +use Aws\Command; +use Aws\Exception\AwsException; use Aws\SesV2\SesV2Client; use Illuminate\Config\Repository; use Illuminate\Container\Container; @@ -10,6 +12,7 @@ use Illuminate\View\Factory; use Mockery as m; use PHPUnit\Framework\TestCase; +use Symfony\Component\Mailer\Exception\TransportException; use Symfony\Component\Mailer\Header\MetadataHeader; use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Email; @@ -78,6 +81,23 @@ public function testSend() (new SesV2Transport($client))->send($message); } + public function testSendError() + { + $message = new Email(); + $message->subject('Foo subject'); + $message->text('Bar body'); + $message->sender('myself@example.com'); + $message->to('me@example.com'); + + $client = m::mock(SesV2Client::class); + $client->shouldReceive('sendEmail')->once() + ->andThrow(new AwsException('Email address is not verified.', new Command('sendRawEmail'))); + + $this->expectException(TransportException::class); + + (new SesV2Transport($client))->send($message); + } + public function testSesV2LocalConfiguration() { $container = new Container;