8000 bug #46603 [Mailer] Fix Error Handling for OhMySMTP Bridge (paul-oms) · symfony/symfony@2faa354 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2faa354

Browse files
committed
bug #46603 [Mailer] Fix Error Handling for OhMySMTP Bridge (paul-oms)
This PR was merged into the 5.4 branch. Discussion ---------- [Mailer] Fix Error Handling for OhMySMTP Bridge | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix N/A <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> The OhMySMTP Bridge does not handle errors correctly, and throws up an `Undefined array key "Message"` error when an API error is encountered (e.g. an invalid API key or Internal Server Error). This PR fixes this by showing the full error response. Commits ------- 3bebc64 [Mailer] Fix Error Handling for OhMySMTP Bridge
2 parents 95c3dcc + 3bebc64 commit 2faa354

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/Symfony/Component/Mailer/Bridge/OhMySmtp/Tests/Transport/OhMySmtpApiTransportTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testSend()
101101
public function testSendThrowsForErrorResponse()
102102
{
103103
$client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface {
104-
return new MockResponse(json_encode(['Message' => 'i\'m a teapot', 'ErrorCode' => 418]), [
104+
return new MockResponse(json_encode(['error' => 'i\'m a teapot']), [
105105
'http_code' => 418,
106106
'response_headers' => [
107107
'content-type' => 'application/json',
@@ -118,7 +118,31 @@ public function testSendThrowsForErrorResponse()
118118
->text('Hello There!');
119119

120120
$this->expectException(HttpTransportException::class);
121-
$this->expectExceptionMessage('Unable to send an email: i\'m a teapot (code 418).');
121+
$this->expectExceptionMessage('Unable to send an email: {"error":"i\'m a teapot"}');
122+
$transport->send($mail);
123+
}
124+
125+
public function testSendThrowsForMultipleErrorResponses()
126+
{
127+
$client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface {
128+
return new MockResponse(json_encode(['errors' => ["to" => "undefined field" ]]), [
129+
'http_code' => 418,
130+
'response_headers' => [
131+
'content-type' => 'application/json',
132+
],
133+
]);
134+
});
135+
$transport = new OhMySmtpApiTransport('KEY', $client);
136+
$transport->setPort(8984);
137+
138+
$mail = new Email();
139+
$mail->subject('Hello!')
140+
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
141+
->from(new Address('fabpot@symfony.com', 'Fabien'))
142+
->text('Hello There!');
143+
144+
$this->expectException(HttpTransportException::class);
145+
$this->expectExceptionMessage('Unable to send an email: {"errors":{"to":"undefined field"}}');
122146
$transport->send($mail);
123147
}
124148

src/Symfony/Component/Mailer/Bridge/OhMySmtp/Transport/OhMySmtpApiTransport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
6767
}
6868

6969
if (200 !== $statusCode) {
70-
throw new HttpTransportException('Unable to send an email: '.$result['Message'].sprintf(' (code %d).', $result['ErrorCode']), $response);
70+
throw new HttpTransportException('Unable to send an email: '.$response->getContent(false), $response);
7171
}
7272

7373
$sentMessage->setMessageId($result['id']);

0 commit comments

Comments
 (0)
0