Skip to content

[Mailer] [MailPace] Fix undefined array key in errors response #50515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Mailer] [MailPace] Fix undefined array key in errors response
  • Loading branch information
flofloflo authored and nicolas-grekas committed Jun 1, 2023
commit 38a7ee534a8f957c54ff28bcb16a148219c439be
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,57 @@ public function testSendThrowsForErrorResponse()
$transport->send($mail);
}

public function testSendThrowsForErrorsResponse()
{
$client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface {
return new MockResponse(json_encode([
'errors' => [
'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'],
],
]), [
'http_code' => 400,
'response_headers' => [
'content-type' => 'application/json',
],
]);
});
$transport = new MailPaceApiTransport('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: 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).');
$transport->send($mail);
}

public function testSendThrowsForInternalServerErrorResponse()
{
$client = new MockHttpClient(static function (string $method, string $url, array $options): ResponseInterface {
return new MockResponse('', ['http_code' => 500]);
});
$transport = new MailPaceApiTransport('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: (code 500).');
$transport->send($mail);
}

public function testTagAndMetadataHeaders()
{
$email = new Email();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,20 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
}

if (200 !== $statusCode) {
throw new HttpTransportException('Unable to send an email: '.$result['error'].sprintf(' (code %d).', $statusCode), $response);
$errorMessage = 'Unable to send an email: ';
if (isset($result['error'])) {
$errorMessage .= $result['error'];
} elseif (isset($result['errors'])) {
$errors = [];
foreach ($result['errors'] as $key => $val) {
$errors[] = $key.': '.implode(' & ', $val);
}
$errorMessage .= implode('; ', $errors);
} else {
$errorMessage .= 'unknown error';
}
$errorMessage .= sprintf(' (code %d).', $statusCode);
throw new HttpTransportException($errorMessage, $response);
}

$sentMessage->setMessageId($result['id']);
Expand Down
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