Extract sending error messages from bridge to strings

The original error is stored under a new key for easier handling an error state.
[MAILPOET-4639]
This commit is contained in:
Jan Lysý
2023-01-06 14:30:16 +01:00
committed by Aschepikov
parent a53e446583
commit 982568445b
3 changed files with 40 additions and 10 deletions

View File

@@ -23,8 +23,6 @@ class MailPoetMapper {
const METHOD = Mailer::METHOD_MAILPOET;
const TEMPORARY_UNAVAILABLE_RETRY_INTERVAL = 300; // seconds
// Bridge message from https://github.com/mailpoet/services-bridge/blob/a3fbf0c1a88abc77840f9ec9f3965e632ce7d8b5/api/messages.rb#L16
const MAILPOET_BRIDGE_DMRAC_ERROR = "Email violates Sender Domain's DMARC policy. Please set up sender authentication.";
/** @var Bridge */
private $bridge;
@@ -78,7 +76,7 @@ class MailPoetMapper {
$resultParsed = json_decode($result['message'], true);
$message = __('Error while sending.', 'mailpoet');
if (!is_array($resultParsed)) {
if ($result['message'] === self::MAILPOET_BRIDGE_DMRAC_ERROR) {
if ($result['error'] === API::ERROR_MESSAGE_DMRAC) {
$message .= $this->getDmarcMessage($result, $sender);
} else {
$message .= ' ' . $result['message'];
@@ -244,19 +242,19 @@ class MailPoetMapper {
* Returns error $message and $operation for API::RESPONSE_CODE_CAN_NOT_SEND
*/
private function getCanNotSendError(array $result, $sender): array {
if ($result['message'] === MailerError::MESSAGE_PENDING_APPROVAL) {
if ($result['error'] === API::ERROR_MESSAGE_PENDING_APPROVAL) {
$operation = MailerError::OPERATION_PENDING_APPROVAL;
$message = $this->getPendingApprovalMessage();
return [$operation, $message];
}
if ($result['message'] === MailerError::MESSAGE_EMAIL_INSUFFICIENT_PRIVILEGES) {
if ($result['error'] === API::ERROR_MESSAGE_INSUFFICIENT_PRIVILEGES) {
$operation = MailerError::OPERATION_INSUFFICIENT_PRIVILEGES;
$message = $this->getInsufficientPrivilegesMessage();
return [$operation, $message];
}
if ($result['message'] === MailerError::MESSAGE_EMAIL_VOLUME_LIMIT_REACHED) {
if ($result['error'] === API::ERROR_MESSAGE_EMAIL_VOLUME_LIMIT_REACHED) {
// Update the current email volume limit from MSS
$premiumKey = $this->settings->get(Bridge::PREMIUM_KEY_SETTING_NAME);
$result = $this->bridge->checkPremiumKey($premiumKey);
@@ -267,7 +265,7 @@ class MailPoetMapper {
return [$operation, $message];
}
if ($result['message'] === MailerError::MESSAGE_EMAIL_NOT_AUTHORIZED) {
if ($result['error'] === API::ERROR_MESSAGE_INVALID_FROM) {
$operation = MailerError::OPERATION_AUTHORIZATION;
$message = $this->getUnauthorizedEmailMessage($sender);
return [$operation, $message];

View File

@@ -4,7 +4,6 @@ namespace MailPoet\Mailer\Methods;
use MailPoet\Config\ServicesChecker;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper;
use MailPoet\Services\AuthorizedEmailsController;
@@ -83,7 +82,7 @@ class MailPoet implements MailerMethod {
} elseif (
!empty($result['code'])
&& $result['code'] === API::RESPONSE_CODE_CAN_NOT_SEND
&& $result['message'] === MailerError::MESSAGE_EMAIL_NOT_AUTHORIZED
&& $result['error'] === API::ERROR_MESSAGE_INVALID_FROM
) {
$this->authorizedEmailsController->checkAuthorizedEmailAddresses();
}

View File

@@ -25,6 +25,16 @@ class API {
const RESPONSE_CODE_PAYLOAD_ERROR = 400;
const RESPONSE_CODE_CAN_NOT_SEND = 403;
// Bridge messages from https://github.com/mailpoet/services-bridge/blob/master/api/messages.rb
public const ERROR_MESSAGE_BANNED = 'Key is valid, but the action is forbidden';
public const ERROR_MESSAGE_INVALID_FROM = 'The email address is not authorized';
public const ERROR_MESSAGE_PENDING_APPROVAL = 'Key is valid, but not approved yet; you can send only to authorized email addresses at the moment';
public const ERROR_MESSAGE_DMRAC = "Email violates Sender Domain's DMARC policy. Please set up sender authentication.";
// Bridge message from https://github.com/mailpoet/services-bridge/blob/master/extensions/authentication/basic_strategy.rb
public const ERROR_MESSAGE_UNAUTHORIZED = 'No valid API key provided';
public const ERROR_MESSAGE_INSUFFICIENT_PRIVILEGES = 'Insufficient privileges';
public const ERROR_MESSAGE_EMAIL_VOLUME_LIMIT_REACHED = 'Email volume limit reached';
private $apiKey;
private $wp;
/** @var LoggerFactory */
@@ -133,7 +143,8 @@ class API {
$this->wp->wpRemoteRetrieveResponseMessage($result);
return [
'status' => self::SENDING_STATUS_SEND_ERROR,
'message' => $response,
'error' => $response,
'message' => $this->getTranslatedErrorMessage($response),
'code' => $responseCode,
];
}
@@ -333,6 +344,28 @@ class API {
return $this->apiKey;
}
public function getTranslatedErrorMessage(string $errorMessage): string {
switch ($errorMessage) {
case self::ERROR_MESSAGE_BANNED:
return __('Key is valid, but the action is forbidden.', 'mailpoet');
case self::ERROR_MESSAGE_INVALID_FROM:
return __('The email address is not authorized.', 'mailpoet');
case self::ERROR_MESSAGE_PENDING_APPROVAL:
return __('Key is valid, but not approved yet; you can send only to authorized email addresses at the moment.', 'mailpoet');
case self::ERROR_MESSAGE_DMRAC:
return __("Email violates Sender Domain's DMARC policy. Please set up sender authentication.", 'mailpoet');
case self::ERROR_MESSAGE_UNAUTHORIZED:
return __('No valid API key provided.', 'mailpoet');
case self::ERROR_MESSAGE_INSUFFICIENT_PRIVILEGES:
return __('Insufficient privileges.', 'mailpoet');
case self::ERROR_MESSAGE_EMAIL_VOLUME_LIMIT_REACHED:
return __('Email volume limit reached.', 'mailpoet');
// when we don't match translation we return the origin
default:
return $errorMessage;
}
}
private function auth() {
return 'Basic ' . base64_encode('api:' . $this->apiKey);
}