Refactor sending methods to use error mappers
We want to add some logic to error handling. This commit extracts error handling code from sending methods classes, which already do a lot of other stuff, to error mappers which are responsible for creating proper error object (MailerError). This error object is a replacement for assoc. array which already had some special keys for certain usecases and can not be properly type hinted. [MAILPOET-1154]
This commit is contained in:
23
lib/Mailer/Methods/ErrorMappers/AmazonSESMapper.php
Normal file
23
lib/Mailer/Methods/ErrorMappers/AmazonSESMapper.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace MailPoet\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
|
||||
class AmazonSESMapper {
|
||||
use ConnectionErrorMapperTrait;
|
||||
|
||||
function getErrorFromException(\Exception $e) {
|
||||
return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, $e->getMessage());
|
||||
}
|
||||
|
||||
function getErrorFromResponse($response, $subscriber, $extra_params) {
|
||||
$response = ($response) ?
|
||||
$response->Error->Message->__toString() :
|
||||
sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_AMAZONSES);
|
||||
if(empty($extra_params['test_email'])) {
|
||||
$response .= sprintf(' %s: %s', __('Unprocessed subscriber', 'mailpoet'), $subscriber);
|
||||
}
|
||||
return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, $response);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace MailPoet\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use MailPoet\Mailer\MailerError;
|
||||
|
||||
trait ConnectionErrorMapperTrait {
|
||||
function getConnectionError($message) {
|
||||
return new MailerError(
|
||||
MailerError::OPERATION_CONNECT,
|
||||
MailerError::LEVEL_HARD,
|
||||
$message
|
||||
);
|
||||
}
|
||||
}
|
80
lib/Mailer/Methods/ErrorMappers/MailPoetMapper.php
Normal file
80
lib/Mailer/Methods/ErrorMappers/MailPoetMapper.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
namespace MailPoet\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Services\Bridge\API;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class MailPoetMapper {
|
||||
use ConnectionErrorMapperTrait;
|
||||
|
||||
const TEMPORARY_UNAVAILABLE_RETRY_INTERVAL = 300; // seconds
|
||||
|
||||
function getInvalidApiKeyError() {
|
||||
return new MailerError(
|
||||
MailerError::OPERATION_SEND,
|
||||
MailerError::LEVEL_HARD,
|
||||
__('MailPoet API key is invalid!', 'mailpoet')
|
||||
);
|
||||
}
|
||||
|
||||
function getErrorForResult(array $result, $subscribers) {
|
||||
$message = $result['message'];
|
||||
$level = MailerError::LEVEL_HARD;
|
||||
$retry_interval = null;
|
||||
|
||||
if(!empty($result['code'])) {
|
||||
switch($result['code']) {
|
||||
case API::RESPONSE_CODE_NOT_ARRAY:
|
||||
$message = __('JSON input is not an array', 'mailpoet');
|
||||
break;
|
||||
case API::RESPONSE_CODE_PAYLOAD_ERROR:
|
||||
$message = $this->parseErrorResponse($result['message'], $subscribers);
|
||||
break;
|
||||
case API::RESPONSE_CODE_TEMPORARY_UNAVAILABLE:
|
||||
$message = __('Email service is temporarily not available, please try again in a few minutes.', 'mailpoet');
|
||||
$retry_interval = self::TEMPORARY_UNAVAILABLE_RETRY_INTERVAL;
|
||||
break;
|
||||
case API::RESPONSE_CODE_KEY_INVALID:
|
||||
case API::RESPONSE_CODE_PAYLOAD_TOO_BIG:
|
||||
default:
|
||||
$message = $result['message'];
|
||||
}
|
||||
}
|
||||
return new MailerError(MailerError::OPERATION_SEND, $level, $message, $retry_interval);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
21
lib/Mailer/Methods/ErrorMappers/PHPMailMapper.php
Normal file
21
lib/Mailer/Methods/ErrorMappers/PHPMailMapper.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace MailPoet\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
|
||||
class PHPMailMapper {
|
||||
use ConnectionErrorMapperTrait;
|
||||
|
||||
function getErrorFromException(\Exception $e) {
|
||||
return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, $e->getMessage());
|
||||
}
|
||||
|
||||
function getErrorForSubscriber($subscriber, $extra_params) {
|
||||
$message = sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_PHPMAIL);
|
||||
if(empty($extra_params['test_email'])) {
|
||||
$message .= sprintf(' %s: %s', __('Unprocessed subscriber', 'mailpoet'), $subscriber);
|
||||
}
|
||||
return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, $message);
|
||||
}
|
||||
}
|
31
lib/Mailer/Methods/ErrorMappers/SMTPMapper.php
Normal file
31
lib/Mailer/Methods/ErrorMappers/SMTPMapper.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
namespace MailPoet\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
|
||||
class SMTPMapper {
|
||||
use ConnectionErrorMapperTrait;
|
||||
|
||||
function getErrorFromException(\Exception $e) {
|
||||
// remove redundant information appended by Swift logger to exception messages
|
||||
$message = explode(PHP_EOL, $e->getMessage());
|
||||
return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, $message[0]);
|
||||
}
|
||||
|
||||
function getErrorFromLog($log, $subscriber, $extra_params = []) {
|
||||
// extract error message from log
|
||||
preg_match('/!! (.*?)>>/ism', $log, $message);
|
||||
if(!empty($message[1])) {
|
||||
$message = $message[1];
|
||||
// remove line breaks from the message due to how logger's dump() method works
|
||||
$message = preg_replace('/\r|\n/', '', $message);
|
||||
} else {
|
||||
$message = sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_SMTP);
|
||||
}
|
||||
if(empty($extra_params['test_email'])) {
|
||||
$message .= sprintf(' %s: %s', __('Unprocessed subscriber', 'mailpoet'), $subscriber);
|
||||
}
|
||||
return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, $message);
|
||||
}
|
||||
}
|
19
lib/Mailer/Methods/ErrorMappers/SendGridMapper.php
Normal file
19
lib/Mailer/Methods/ErrorMappers/SendGridMapper.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace MailPoet\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
|
||||
class SendGridMapper {
|
||||
use ConnectionErrorMapperTrait;
|
||||
|
||||
function getErrorFromResponse($response, $subscriber, $extra_params) {
|
||||
$response = (!empty($response['errors'][0])) ?
|
||||
$response['errors'][0] :
|
||||
sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_SENDGRID);
|
||||
if(empty($extra_params['test_email'])) {
|
||||
$response .= sprintf(' %s: %s', __('Unprocessed subscriber', 'mailpoet'), $subscriber);
|
||||
}
|
||||
return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, $response);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user