Add soft level errors distinction for SMTP method
[MAILPOET-1154]
This commit is contained in:
@ -8,10 +8,25 @@ use MailPoet\Mailer\SubscriberError;
|
|||||||
class SMTPMapper {
|
class SMTPMapper {
|
||||||
use ConnectionErrorMapperTrait;
|
use ConnectionErrorMapperTrait;
|
||||||
|
|
||||||
function getErrorFromException(\Exception $e) {
|
/**
|
||||||
|
* @see https://swiftmailer.symfony.com/docs/sending.html
|
||||||
|
* @return MailerError
|
||||||
|
*/
|
||||||
|
function getErrorFromException(\Exception $e, $subscriber, $extra_params = []) {
|
||||||
// remove redundant information appended by Swift logger to exception messages
|
// remove redundant information appended by Swift logger to exception messages
|
||||||
$message = explode(PHP_EOL, $e->getMessage());
|
$message = explode(PHP_EOL, $e->getMessage());
|
||||||
return new MailerError(MailerError::OPERATION_SEND, MailerError::LEVEL_HARD, $message[0]);
|
|
||||||
|
$level = MailerError::LEVEL_HARD;
|
||||||
|
if($e instanceof \Swift_RfcComplianceException) {
|
||||||
|
$level = MailerError::LEVEL_SOFT;
|
||||||
|
}
|
||||||
|
|
||||||
|
$subscriber_errors = [];
|
||||||
|
if(empty($extra_params['test_email'])) {
|
||||||
|
$subscriber_errors[] = new SubscriberError($subscriber, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new MailerError(MailerError::OPERATION_SEND, $level, $message[0], null, $subscriber_errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getErrorFromLog($log, $subscriber, $extra_params = []) {
|
function getErrorFromLog($log, $subscriber, $extra_params = []) {
|
||||||
|
@ -49,7 +49,7 @@ class SMTP {
|
|||||||
$result = $this->mailer->send($message);
|
$result = $this->mailer->send($message);
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
return Mailer::formatMailerErrorResult(
|
return Mailer::formatMailerErrorResult(
|
||||||
$this->error_mapper->getErrorFromException($e)
|
$this->error_mapper->getErrorFromException($e, $subscriber, $extra_params)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if($result === 1) {
|
if($result === 1) {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
|
namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
|
||||||
|
|
||||||
use MailPoet\Mailer\Mailer;
|
use MailPoet\Mailer\Mailer;
|
||||||
|
use MailPoet\Mailer\MailerError;
|
||||||
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
|
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
|
||||||
|
|
||||||
class SMTPMapperTest extends \MailPoetTest {
|
class SMTPMapperTest extends \MailPoetTest {
|
||||||
@ -18,9 +19,17 @@ class SMTPMapperTest extends \MailPoetTest {
|
|||||||
. 'Log data:' . PHP_EOL
|
. 'Log data:' . PHP_EOL
|
||||||
. '++ Starting Swift_SmtpTransport' . PHP_EOL
|
. '++ Starting Swift_SmtpTransport' . PHP_EOL
|
||||||
. '!! Connection could not be established with host localhost [Connection refused #111] (code: 0)';
|
. '!! Connection could not be established with host localhost [Connection refused #111] (code: 0)';
|
||||||
$error = $this->mapper->getErrorFromException(new \Exception($message));
|
$error = $this->mapper->getErrorFromException(new \Exception($message), 'john@rambo.com');
|
||||||
expect($error->getMessage())
|
expect($error->getMessage())
|
||||||
->equals('Connection could not be established with host localhost [Connection refused #111]');
|
->equals('Connection could not be established with host localhost [Connection refused #111]');
|
||||||
|
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||||
|
expect($error->getSubscriberErrors()[0]->getEmail())->equals('john@rambo.com');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testItCreatesSoftErrorForInvalidEmail() {
|
||||||
|
$message = 'Invalid email';
|
||||||
|
$error = $this->mapper->getErrorFromException(new \Swift_RfcComplianceException($message), 'john@rambo.com');
|
||||||
|
expect($error->getLevel())->equals(MailerError::LEVEL_SOFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanProcessLogMessageWhenOneExists() {
|
function testItCanProcessLogMessageWhenOneExists() {
|
||||||
|
Reference in New Issue
Block a user