Skip to content

Commit a88e5f8

Browse files
bug #50515 [Mailer] [MailPace] Fix undefined array key in errors response (Florian Heller)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [Mailer] [MailPace] Fix undefined array key in errors response | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | none | License | MIT | Doc PR | - This PR fixes a bug which occurs when MailPace returns an `errors` response instead of `error` (see https://docs.mailpace.com/reference/responses/ for possible responses). Without this fix, the `errors` response leads to the following exception: `ErrorException: Undefined array key "error"` I added two tests to verify that: - responses with multiple errors are returned as readable error message - empty responses generate a readable error message To fix the bug I added a check for the existence of the `error`/`errors` key in the `$result` variable and a fallback just in case that both are not present. Commits ------- 38a7ee5 [Mailer] [MailPace] Fix undefined array key in errors response
2 parents e901262 + 38a7ee5 commit a88e5f8

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/Symfony/Component/Mailer/Bridge/MailPace/Tests/Transport/MailPaceApiTransportTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,57 @@ public function testSendThrowsForErrorResponse()
121121
$transport->send($mail);
122122
}
123123

124+
public function testSendThrowsForErrorsResponse()
125+
{
126+
$client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface {
127+
return new MockResponse(json_encode([
128+
'errors' => [
129+
'to' => [
130+
'contains a blocked address',
131+
'number of email addresses exceeds maximum volume',
132+
],
133+
'attachments.name' => ['Extension file type blocked, see Docs for full list of allowed file types'],
134+
],
135+
]), [
136+
'http_code' => 400,
137+
'response_headers' => [
138+
'content-type' => 'application/json',
139+
],
140+
]);
141+
});
142+
$transport = new MailPaceApiTransport('KEY', $client);
143+
$transport->setPort(8984);
144+
145+
$mail = new Email();
146+
$mail->subject('Hello!')
147+
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
148+
->from(new Address('fabpot@symfony.com', 'Fabien'))
149+
->text('Hello There!');
150+
151+
$this->expectException(HttpTransportException::class);
152+
$this->expectExceptionMessage('Unable to send an email: to: contains a blocked address & number of email addresses exceeds maximum volume; attachments.name: Extension file type blocked, see Docs for full list of allowed file types (code 400).');
153+
$transport->send($mail);
154+
}
155+
156+
public function testSendThrowsForInternalServerErrorResponse()
157+
{
158+
$client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface {
159+
return new MockResponse('', ['http_code' => 500]);
160+
});
161+
$transport = new MailPaceApiTransport('KEY', $client);
162+
$transport->setPort(8984);
163+
164+
$mail = new Email();
165+
$mail->subject('Hello!')
166+
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
167+
->from(new Address('fabpot@symfony.com', 'Fabien'))
168+
->text('Hello There!');
169+
170+
$this->expectException(HttpTransportException::class);
171+
$this->expectExceptionMessage('Unable to send an email: (code 500).');
172+
$transport->send($mail);
173+
}
174+
124175
public function testTagAndMetadataHeaders()
125176
{
126177
$email = new Email();

src/Symfony/Component/Mailer/Bridge/MailPace/Transport/MailPaceApiTransport.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,20 @@ 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['error'].sprintf(' (code %d).', $statusCode), $response);
70+
$errorMessage = 'Unable to send an email: ';
71+
if (isset($result['error'])) {
72+
$errorMessage .= $result['error'];
73+
} elseif (isset($result['errors'])) {
74+
$errors = [];
75+
foreach ($result['errors'] as $key => $val) {
76+
$errors[] = $key.': '.implode(' & ', $val);
77+
}
78+
$errorMessage .= implode('; ', $errors);
79+
} else {
80+
$errorMessage .= 'unknown error';
81+
}
82+
$errorMessage .= sprintf(' (code %d).', $statusCode);
83+
throw new HttpTransportException($errorMessage, $response);
7184
}
7285

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy