Improve error reporting for MSS sending
[MAILPOET-1445]
This commit is contained in:
@@ -9,6 +9,9 @@ use MailPoet\Services\Bridge\API;
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class MailPoet {
|
||||
|
||||
const TEMPORARY_UNAVAILABLE_RETRY_INTERVAL = 300; // seconds
|
||||
|
||||
public $api;
|
||||
public $sender;
|
||||
public $reply_to;
|
||||
@@ -34,16 +37,37 @@ class MailPoet {
|
||||
case API::SENDING_STATUS_CONNECTION_ERROR:
|
||||
return Mailer::formatMailerConnectionErrorResult($result['message']);
|
||||
case API::SENDING_STATUS_SEND_ERROR:
|
||||
if(!empty($result['code']) && $result['code'] === API::RESPONSE_CODE_KEY_INVALID) {
|
||||
Bridge::invalidateKey();
|
||||
}
|
||||
return Mailer::formatMailerSendErrorResult($result['message']);
|
||||
return $this->processSendError($result, $subscriber);
|
||||
case API::SENDING_STATUS_OK:
|
||||
default:
|
||||
return Mailer::formatMailerSendSuccessResult();
|
||||
}
|
||||
}
|
||||
|
||||
function processSendError($result, $subscriber) {
|
||||
if(!empty($result['code'])) {
|
||||
switch($result['code']) {
|
||||
case API::RESPONSE_CODE_NOT_ARRAY:
|
||||
return Mailer::formatMailerSendErrorResult(__('JSON input is not an array', 'mailpoet'));
|
||||
case API::RESPONSE_CODE_PAYLOAD_TOO_BIG:
|
||||
return Mailer::formatMailerSendErrorResult($result['message']);
|
||||
case API::RESPONSE_CODE_PAYLOAD_ERROR:
|
||||
$error = $this->parseErrorResponse($result['message'], $subscriber);
|
||||
return Mailer::formatMailerSendErrorResult($error);
|
||||
case API::RESPONSE_CODE_TEMPORARY_UNAVAILABLE:
|
||||
$error = Mailer::formatMailerSendErrorResult(__('Email service is temporarily not available, please try again in a few minutes.', 'mailpoet'));
|
||||
$error['retry_interval'] = self::TEMPORARY_UNAVAILABLE_RETRY_INTERVAL;
|
||||
return $error;
|
||||
case API::RESPONSE_CODE_KEY_INVALID:
|
||||
Bridge::invalidateKey();
|
||||
break;
|
||||
default:
|
||||
return Mailer::formatMailerSendErrorResult($result['message']);
|
||||
}
|
||||
}
|
||||
return Mailer::formatMailerSendErrorResult($result['message']);
|
||||
}
|
||||
|
||||
function processSubscriber($subscriber) {
|
||||
preg_match('!(?P<name>.*?)\s<(?P<email>.*?)>!', $subscriber, $subscriber_data);
|
||||
if(!isset($subscriber_data['email'])) {
|
||||
@@ -104,4 +128,37 @@ class MailPoet {
|
||||
}
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
|
||||
private function parseErrorResponse($result, $subscriber) {
|
||||
$result_parsed = json_decode($result, true);
|
||||
$errors = [];
|
||||
if(is_array($result_parsed)) {
|
||||
foreach($result_parsed as $result_error) {
|
||||
$errors[] = $this->processSingleSubscriberError($result_error, $subscriber);
|
||||
}
|
||||
}
|
||||
if(!empty($errors)) {
|
||||
return __('Error while sending: ', 'mailpoet') . join(', ', $errors);
|
||||
} else {
|
||||
return __('Error while sending newsletters. ', 'mailpoet') . $result;
|
||||
}
|
||||
}
|
||||
|
||||
private function processSingleSubscriberError($result_error, $subscriber) {
|
||||
$error = '';
|
||||
if(is_array($result_error)) {
|
||||
$subscriber_errors = [];
|
||||
if(isset($result_error['errors']) && is_array($result_error['errors'])) {
|
||||
array_walk_recursive($result_error['errors'], function($item) use (&$subscriber_errors) {
|
||||
$subscriber_errors[] = $item;
|
||||
});
|
||||
}
|
||||
$error .= join(', ', $subscriber_errors);
|
||||
|
||||
if(isset($result_error['index']) && isset($subscriber[$result_error['index']])) {
|
||||
$error = '(' . $subscriber[$result_error['index']] . ': ' . $error . ')';
|
||||
}
|
||||
}
|
||||
return $error;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user