From 51a0899698be108fdeb1be847106c86d21d9b18d Mon Sep 17 00:00:00 2001 From: Brezo Cordero <8002881+brezocordero@users.noreply.github.com> Date: Thu, 16 Sep 2021 13:09:00 -0500 Subject: [PATCH] Mark most SMTP transport errors as soft [MAILPOET-3171] --- lib/Mailer/Methods/ErrorMappers/SMTPMapper.php | 7 +++++++ .../Methods/ErrorMappers/SMTPMapperTest.php | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/Mailer/Methods/ErrorMappers/SMTPMapper.php b/lib/Mailer/Methods/ErrorMappers/SMTPMapper.php index fe3242e5fd..117d87a040 100644 --- a/lib/Mailer/Methods/ErrorMappers/SMTPMapper.php +++ b/lib/Mailer/Methods/ErrorMappers/SMTPMapper.php @@ -7,6 +7,7 @@ use MailPoet\Mailer\MailerError; use MailPoet\Mailer\SubscriberError; use MailPoet\WP\Functions as WPFunctions; use MailPoetVendor\Swift_RfcComplianceException; +use MailPoetVendor\Swift_TransportException; class SMTPMapper { use BlacklistErrorMapperTrait; @@ -21,11 +22,17 @@ class SMTPMapper { public function getErrorFromException(\Exception $e, $subscriber) { // remove redundant information appended by Swift logger to exception messages $message = explode(PHP_EOL, $e->getMessage()); + $code = $e->getCode(); $level = MailerError::LEVEL_HARD; if ($e instanceof Swift_RfcComplianceException) { $level = MailerError::LEVEL_SOFT; } + + if ($e instanceof Swift_TransportException && (($code < 500) || ($code > 504))) { + $level = MailerError::LEVEL_SOFT; + } + $subscriberErrors = [new SubscriberError($subscriber, null)]; return new MailerError(MailerError::OPERATION_SEND, $level, $message[0], null, $subscriberErrors); } diff --git a/tests/unit/Mailer/Methods/ErrorMappers/SMTPMapperTest.php b/tests/unit/Mailer/Methods/ErrorMappers/SMTPMapperTest.php index 17a40de603..3e436266ef 100644 --- a/tests/unit/Mailer/Methods/ErrorMappers/SMTPMapperTest.php +++ b/tests/unit/Mailer/Methods/ErrorMappers/SMTPMapperTest.php @@ -42,6 +42,24 @@ class SMTPMapperTest extends \MailPoetUnitTest { expect($error->getLevel())->equals(MailerError::LEVEL_SOFT); } + public function testItCreatesSoftErrorForMostTransportExceptions() { + $message = 'Connection could not be established with host localhost [Connection timed out]' . PHP_EOL + . 'Log data:' . PHP_EOL + . '++ Starting Swift_SmtpTransport' . PHP_EOL + . '!! Connection could not be established with host localhost [Connection timed out] (code: 463)'; + $error = $this->mapper->getErrorFromException(new Swift_RfcComplianceException($message), 'john@rambo.com'); + expect($error->getLevel())->equals(MailerError::LEVEL_SOFT); + } + + public function testItCreatesHardErrorForTransportException500to504() { + $message = 'Bad sequence of commands' . PHP_EOL + . 'Log data:' . PHP_EOL + . '++ Starting Swift_SmtpTransport' . PHP_EOL + . '!! Bad sequence of commands (code: 503)'; + $error = $this->mapper->getErrorFromException(new Swift_RfcComplianceException($message), 'john@rambo.com'); + expect($error->getLevel())->equals(MailerError::LEVEL_SOFT); + } + public function testItCanProcessLogMessageWhenOneExists() { $log = '++ Swift_SmtpTransport started' . PHP_EOL . '>> MAIL FROM:' . PHP_EOL