Extract domain verification 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 19:35:23 +01:00
committed by Aschepikov
parent 2ace6da75b
commit 07954dd968
4 changed files with 36 additions and 26 deletions

View File

@ -348,7 +348,7 @@ class Settings extends APIEndpoint {
if (!$response['ok']) { if (!$response['ok']) {
// sender domain verification error. probably an improper setup // sender domain verification error. probably an improper setup
return $this->badRequest([ return $this->badRequest([
APIError::BAD_REQUEST => $response['error'] ?? __('Sender domain verification failed.', 'mailpoet'), APIError::BAD_REQUEST => $response['message'] ?? __('Sender domain verification failed.', 'mailpoet'),
], $response); ], $response);
} }

View File

@ -2,6 +2,7 @@
namespace MailPoet\Services; namespace MailPoet\Services;
use MailPoet\Services\Bridge\API;
use MailPoet\Util\DmarcPolicyChecker; use MailPoet\Util\DmarcPolicyChecker;
class AuthorizedSenderDomainController { class AuthorizedSenderDomainController {
@ -125,18 +126,14 @@ class AuthorizedSenderDomainController {
throw new \InvalidArgumentException(self::AUTHORIZED_SENDER_DOMAIN_ERROR_ALREADY_VERIFIED); throw new \InvalidArgumentException(self::AUTHORIZED_SENDER_DOMAIN_ERROR_ALREADY_VERIFIED);
} }
$finalData = $this->bridge->verifyAuthorizedSenderDomain($domain); $response = $this->bridge->verifyAuthorizedSenderDomain($domain);
if ($finalData && isset($finalData['error']) && !isset($finalData['dns'])) { // API response contains status, but we need to check that dns array is not included
// verify api response returns if ($response['status'] === API::AUTHORIZED_DOMAIN_STATUS_ERROR && !isset($response['dns'])) {
// ok: bool, throw new \InvalidArgumentException($response['message']);
// error: string,
// dns: array
// due to this, we need to make sure this is an actual server (or other user) error and not a verification error
throw new \InvalidArgumentException($finalData['error']);
} }
return $finalData; return $response;
} }
/** /**

View File

@ -189,11 +189,9 @@ class Bridge {
* @see https://github.com/mailpoet/services-bridge#verify-a-sender-domain * @see https://github.com/mailpoet/services-bridge#verify-a-sender-domain
*/ */
public function verifyAuthorizedSenderDomain(string $domain): array { public function verifyAuthorizedSenderDomain(string $domain): array {
$data = $this return $this
->getApi($this->settings->get(self::API_KEY_SETTING_NAME)) ->getApi($this->settings->get(self::API_KEY_SETTING_NAME))
->verifyAuthorizedSenderDomain($domain); ->verifyAuthorizedSenderDomain($domain);
return $data;
} }
public function checkMSSKey($apiKey) { public function checkMSSKey($apiKey) {

View File

@ -9,6 +9,8 @@ use WP_Error;
class API { class API {
const AUTHORIZED_EMAIL_STATUS_OK = 'ok'; const AUTHORIZED_EMAIL_STATUS_OK = 'ok';
const AUTHORIZED_EMAIL_STATUS_ERROR = 'authorized_email_error'; const AUTHORIZED_EMAIL_STATUS_ERROR = 'authorized_email_error';
const AUTHORIZED_DOMAIN_STATUS_OK = 'ok';
const AUTHORIZED_DOMAIN_STATUS_ERROR = 'authorized_domain_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';
@ -40,6 +42,9 @@ class API {
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_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_INVALID = 'Invalid email.';
public const ERROR_MESSAGE_AUTHORIZED_EMAIL_ALREADY_ADDED = 'This email was already added to the list.'; public const ERROR_MESSAGE_AUTHORIZED_EMAIL_ALREADY_ADDED = 'This email was already added to the list.';
// Proxy request `sender_domain_verify` from shop https://github.com/mailpoet/shop/blob/master/routes/hooks/sending/v1/index.js#L137
public const ERROR_MESSAGE_AUTHORIZED_DOMAIN_VERIFY_NOT_FOUND = 'Domain not found';
public const ERROR_MESSAGE_AUTHORIZED_DOMAIN_VERIFY_FAILED = 'Some DNS records were not set up correctly. Please check the records again. You may need to wait up to 24 hours for DNS changes to propagate.';
private $apiKey; private $apiKey;
private $wp; private $wp;
@ -312,28 +317,33 @@ class API {
null null
); );
$code = $this->wp->wpRemoteRetrieveResponseCode($result); $responseCode = $this->wp->wpRemoteRetrieveResponseCode($result);
$rawResponseBody = $this->wp->wpRemoteRetrieveBody($result); $rawResponseBody = $this->wp->wpRemoteRetrieveBody($result);
$responseBody = json_decode($rawResponseBody, true); $responseBody = json_decode($rawResponseBody, true);
$isSuccess = $code === 200; if ($responseCode !== 200) {
if ($responseCode === 400) {
if (!$isSuccess) { // we need to return the body as it is, but for consistency we add status and translated error message
if ($code === 400) { $response = is_array($responseBody) ? $responseBody : [];
// we need to return the body as it is $response['status'] = self::AUTHORIZED_DOMAIN_STATUS_ERROR;
return is_array($responseBody) ? $responseBody : []; $response['message'] = $this->getTranslatedErrorMessage($response['error']);
return $response;
} }
$logData = [ $logData = [
'code' => $code, 'code' => $responseCode,
'error' => is_wp_error($result) ? $result->get_error_message() : $rawResponseBody, 'error' => is_wp_error($result) ? $result->get_error_message() : $rawResponseBody,
]; ];
$this->loggerFactory->getLogger(LoggerFactory::TOPIC_BRIDGE)->error('verifyAuthorizedSenderDomain API call failed.', $logData); $this->loggerFactory->getLogger(LoggerFactory::TOPIC_BRIDGE)->error('verifyAuthorizedSenderDomain API call failed.', $logData);
// translators: %d will be replaced by an error code // translators: %d will be replaced by an 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($responseBody) && isset($responseBody['error']) ? $responseBody['error'] : $fallbackError; return [
return ['error' => $errorData, 'status' => false]; 'status' => self::AUTHORIZED_DOMAIN_STATUS_ERROR,
'code' => $responseCode,
'error' => is_array($responseBody) && isset($responseBody['error']) ? $responseBody['error'] : $fallbackError,
'message' => is_array($responseBody) && isset($responseBody['error']) ? $this->getTranslatedErrorMessage($responseBody['error']) : $fallbackError,
];
} }
if (!is_array($responseBody)) { if (!is_array($responseBody)) {
@ -341,6 +351,7 @@ class API {
return []; return [];
} }
$responseBody['status'] = self::AUTHORIZED_DOMAIN_STATUS_OK;
return $responseBody; return $responseBody;
} }
@ -374,6 +385,10 @@ class API {
return __('Invalid email.', 'mailpoet'); return __('Invalid email.', 'mailpoet');
case self::ERROR_MESSAGE_AUTHORIZED_EMAIL_ALREADY_ADDED: case self::ERROR_MESSAGE_AUTHORIZED_EMAIL_ALREADY_ADDED:
return __('This email was already added to the list.', 'mailpoet'); return __('This email was already added to the list.', 'mailpoet');
case self::ERROR_MESSAGE_AUTHORIZED_DOMAIN_VERIFY_NOT_FOUND:
return __('Domain not found.', 'mailpoet');
case self::ERROR_MESSAGE_AUTHORIZED_DOMAIN_VERIFY_FAILED:
return __('Some DNS records were not set up correctly. Please check the records again. You may need to wait up to 24 hours for DNS changes to propagate.', '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;