Extract email authorization error messages from bridge to strings

As a part of those changes I tried to unify the array key with error messages.
[MAILPOET-4639]
This commit is contained in:
Jan Lysý
2023-01-06 16:46:53 +01:00
committed by Aschepikov
parent 982568445b
commit 2ace6da75b
3 changed files with 34 additions and 23 deletions

View File

@@ -7,7 +7,7 @@ use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MailerError; use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\MailerLog; use MailPoet\Mailer\MailerLog;
use MailPoet\Newsletter\NewslettersRepository; use MailPoet\Newsletter\NewslettersRepository;
use MailPoet\Services\AuthorizedSenderDomainController; use MailPoet\Services\Bridge\API;
use MailPoet\Settings\SettingsController; use MailPoet\Settings\SettingsController;
use MailPoet\Util\Helpers; use MailPoet\Util\Helpers;
@@ -95,13 +95,12 @@ class AuthorizedEmailsController {
throw new \InvalidArgumentException(self::AUTHORIZED_EMAIL_ERROR_PENDING_CONFIRMATION); throw new \InvalidArgumentException(self::AUTHORIZED_EMAIL_ERROR_PENDING_CONFIRMATION);
} }
$finalData = $this->bridge->createAuthorizedEmailAddress($email); $response = $this->bridge->createAuthorizedEmailAddress($email);
if ($response['status'] === API::AUTHORIZED_EMAIL_STATUS_ERROR) {
if ($finalData && isset($finalData['error'])) { throw new \InvalidArgumentException($response['message']);
throw new \InvalidArgumentException($finalData['error']);
} }
return $finalData; return $response;
} }
public function isEmailAddressAuthorized(string $email): bool { public function isEmailAddressAuthorized(string $email): bool {

View File

@@ -134,12 +134,10 @@ class Bridge {
/** /**
* Create Authorized Email Address * Create Authorized Email Address
*/ */
public function createAuthorizedEmailAddress(string $emailAdress) { public function createAuthorizedEmailAddress(string $emailAddress) {
$data = $this return $this
->getApi($this->settings->get(self::API_KEY_SETTING_NAME)) ->getApi($this->settings->get(self::API_KEY_SETTING_NAME))
->createAuthorizedEmailAddress($emailAdress); ->createAuthorizedEmailAddress($emailAddress);
return $data;
} }
/** /**
@@ -179,8 +177,8 @@ class Bridge {
*/ */
public function createAuthorizedSenderDomain(string $domain): array { public function createAuthorizedSenderDomain(string $domain): array {
$data = $this $data = $this
->getApi($this->settings->get(self::API_KEY_SETTING_NAME)) ->getApi($this->settings->get(self::API_KEY_SETTING_NAME))
->createAuthorizedSenderDomain($domain); ->createAuthorizedSenderDomain($domain);
return $data['dns'] ?? $data; return $data['dns'] ?? $data;
} }

View File

@@ -7,6 +7,8 @@ use MailPoet\WP\Functions as WPFunctions;
use WP_Error; use WP_Error;
class API { class API {
const AUTHORIZED_EMAIL_STATUS_OK = 'ok';
const AUTHORIZED_EMAIL_STATUS_ERROR = 'authorized_email_error';
const SENDING_STATUS_OK = 'ok'; const SENDING_STATUS_OK = 'ok';
const SENDING_STATUS_CONNECTION_ERROR = 'connection_error'; const SENDING_STATUS_CONNECTION_ERROR = 'connection_error';
const SENDING_STATUS_SEND_ERROR = 'send_error'; const SENDING_STATUS_SEND_ERROR = 'send_error';
@@ -34,6 +36,10 @@ class API {
public const ERROR_MESSAGE_UNAUTHORIZED = 'No valid API key provided'; public const ERROR_MESSAGE_UNAUTHORIZED = 'No valid API key provided';
public const ERROR_MESSAGE_INSUFFICIENT_PRIVILEGES = 'Insufficient privileges'; public const ERROR_MESSAGE_INSUFFICIENT_PRIVILEGES = 'Insufficient privileges';
public const ERROR_MESSAGE_EMAIL_VOLUME_LIMIT_REACHED = 'Email volume limit reached'; public const ERROR_MESSAGE_EMAIL_VOLUME_LIMIT_REACHED = 'Email volume limit reached';
// Proxy request `authorized_email_address` from shop https://github.com/mailpoet/shop/blob/master/routes/hooks/sending/v1/index.js#L65
public const ERROR_MESSAGE_AUTHORIZED_EMAIL_NO_FREE = 'You cannot use a free email address. Please use an address from your websites domain, for example.';
public const ERROR_MESSAGE_AUTHORIZED_EMAIL_INVALID = 'Invalid email.';
public const ERROR_MESSAGE_AUTHORIZED_EMAIL_ALREADY_ADDED = 'This email was already added to the list.';
private $apiKey; private $apiKey;
private $wp; private $wp;
@@ -196,9 +202,8 @@ class API {
/** /**
* Create Authorized Email Address * Create Authorized Email Address
* *
* returns ['status' => true] if done or an array of error messages ['error' => $errorBody, 'status' => false]
* @param string $emailAddress * @param string $emailAddress
* @return array * @return array{status: string, code?: int, error?: string, message?: string}
*/ */
public function createAuthorizedEmailAddress(string $emailAddress): array { public function createAuthorizedEmailAddress(string $emailAddress): array {
$body = ['email' => $emailAddress]; $body = ['email' => $emailAddress];
@@ -207,26 +212,29 @@ class API {
$body $body
); );
$code = $this->wp->wpRemoteRetrieveResponseCode($result); $responseCode = $this->wp->wpRemoteRetrieveResponseCode($result);
$isSuccess = $code === self::RESPONSE_CODE_CREATED;
if (!$isSuccess) { if ($responseCode !== self::RESPONSE_CODE_CREATED) {
$errorBody = $this->wp->wpRemoteRetrieveBody($result); $errorBody = $this->wp->wpRemoteRetrieveBody($result);
$logData = [ $logData = [
'code' => $code, 'code' => $responseCode,
'error' => is_wp_error($result) ? $result->get_error_message() : $errorBody, 'error' => is_wp_error($result) ? $result->get_error_message() : $errorBody,
]; ];
$this->loggerFactory->getLogger(LoggerFactory::TOPIC_BRIDGE)->error('CreateAuthorizedEmailAddress API call failed.', $logData); $this->loggerFactory->getLogger(LoggerFactory::TOPIC_BRIDGE)->error('CreateAuthorizedEmailAddress API call failed.', $logData);
$errorResponseData = json_decode($errorBody, true); $errorResponseData = json_decode($errorBody, true);
// translators: %d is the error code. // translators: %d is the error code.
$fallbackError = sprintf(__('An error has happened while performing a request, the server has responded with response code %d', 'mailpoet'), $code); $fallbackError = sprintf(__('An error has happened while performing a request, the server has responded with response code %d', 'mailpoet'), $responseCode);
$errorData = is_array($errorResponseData) && isset($errorResponseData['error']) ? $errorResponseData['error'] : $fallbackError; return [
return ['error' => $errorData, 'status' => false]; 'status' => self::AUTHORIZED_EMAIL_STATUS_ERROR,
'code' => $responseCode,
'error' => is_array($errorResponseData) && isset($errorResponseData['error']) ? $errorResponseData['error'] : $fallbackError,
'message' => is_array($errorResponseData) && isset($errorResponseData['error']) ? $this->getTranslatedErrorMessage($errorResponseData['error']) : $fallbackError,
];
} }
return ['status' => $isSuccess]; return ['status' => self::AUTHORIZED_EMAIL_STATUS_OK];
} }
/** /**
@@ -360,6 +368,12 @@ class API {
return __('Insufficient privileges.', 'mailpoet'); return __('Insufficient privileges.', 'mailpoet');
case self::ERROR_MESSAGE_EMAIL_VOLUME_LIMIT_REACHED: case self::ERROR_MESSAGE_EMAIL_VOLUME_LIMIT_REACHED:
return __('Email volume limit reached.', 'mailpoet'); return __('Email volume limit reached.', 'mailpoet');
case self::ERROR_MESSAGE_AUTHORIZED_EMAIL_NO_FREE:
return __('You cannot use a free email address. Please use an address from your websites domain, for example.', 'mailpoet');
case self::ERROR_MESSAGE_AUTHORIZED_EMAIL_INVALID:
return __('Invalid email.', 'mailpoet');
case self::ERROR_MESSAGE_AUTHORIZED_EMAIL_ALREADY_ADDED:
return __('This email was already added to the list.', 'mailpoet');
// when we don't match translation we return the origin // when we don't match translation we return the origin
default: default:
return $errorMessage; return $errorMessage;