Skip to content

Commit

Permalink
Merge pull request #163 from Webador/improve-sparkpost-exceptions
Browse files Browse the repository at this point in the history
Improve SparkPost exceptions
  • Loading branch information
villermen authored Nov 22, 2024
2 parents 94813d9 + c4d274b commit 5572e3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Here are the currently supported services:
* [Mailgun](http://www.mailgun.com) (complete)
* [Postmark](https://postmarkapp.com) (complete)
* [Postage](http://postageapp.com) (complete)
* [Send Grid](http://sendgrid.com) (nearly complete)
* [Spark Post](http://sparkpost.com) (nearly complete)
* [SendGrid](http://sendgrid.com) (nearly complete)
* [SparkPost](http://sparkpost.com) (nearly complete)
* [Amazon SES](http://aws.amazon.com/ses) (nearly complete, attachments are missing)
* [Mandrill](http://mandrill.com) (complete, but please don't use this party, as Mailchimp / Mandrill do not actively maintain this service)

Expand Down
43 changes: 19 additions & 24 deletions src/Service/SparkPostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,32 +524,27 @@ private function parseResponse(HttpResponse $response, array $successCodes = [])
return $result;
}

// There is a 4xx error
if ($response->isClientError()) {
if (isset($result['errors']) && is_array($result['errors'])) {
$message = implode(', ', array_map(
function ($error) {
return $error['message'];
},
$result['errors']
));
} elseif (isset($result['error'])) {
$message = $result['error'];
} else {
$message = 'Unknown error';
}

throw new RuntimeException(
sprintf(
'An error occurred on SparkPost (http code %s), messages: %s',
$response->getStatusCode(),
$message
)
);
if (isset($result['errors']) && is_array($result['errors'])) {
$message = implode(', ', array_map(
function ($error) {
return $error['message'];
},
$result['errors']
));
} elseif (isset($result['error'])) {
$message = $result['error'];
} else {
$message = 'Unknown error';
}

// There is a 5xx error
throw new RuntimeException('SparkPost server error, please try again');
throw new RuntimeException(
sprintf(
'An error occurred on SparkPost (http code %s), messages: %s',
$response->getStatusCode(),
$message
),
$response->getStatusCode()
);
}

public function previewTemplate(
Expand Down

0 comments on commit 5572e3d

Please sign in to comment.