Skip to content

Commit 81f6d0c

Browse files
committed
#329: moved error message from error-level logging to a new parent exception wrapping the original exception
1 parent ec83e32 commit 81f6d0c

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

modules/simple-java-mail/src/main/java/org/simplejavamail/mailer/internal/MailerException.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ class MailerException extends MailException {
1111
static final String ERROR_READING_SMIME_FROM_INPUTSTREAM = "Was unable to read S/MIME data from input stream";
1212
static final String ERROR_READING_FROM_FILE = "Error reading from file: %s";
1313
static final String INVALID_PROXY_SLL_COMBINATION = "Proxy is not supported for SSL connections (this is a limitation by the underlying JavaMail framework)";
14-
static final String GENERIC_ERROR = "Third party error";
15-
static final String INVALID_ENCODING = "Encoding not accepted";
1614
static final String ERROR_CONNECTING_SMTP_SERVER = "Was unable to connect to SMTP server";
15+
static final String GENERIC_ERROR = "Failed to send email [%s], reason: Third party error";
16+
static final String INVALID_ENCODING = "Failed to send email [%s], reason: Encoding not accepted";
17+
static final String UNKNOWN_ERROR = "Failed to send email [%s], reason: Unknown error";
1718

1819
MailerException(@SuppressWarnings("SameParameterValue") final String message) {
1920
super(message);

modules/simple-java-mail/src/main/java/org/simplejavamail/mailer/internal/SendMailClosure.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import java.io.UnsupportedEncodingException;
1717
import java.util.concurrent.atomic.AtomicInteger;
1818

19+
import static java.lang.String.format;
1920
import static org.simplejavamail.converter.EmailConverter.mimeMessageToEML;
21+
import static org.simplejavamail.mailer.internal.MailerException.*;
2022

2123
/**
2224
* Separate closure that can be executed directly or from a thread.
@@ -67,20 +69,19 @@ public void executeClosure() {
6769
TransportRunner.sendMessage(operationalConfig.getClusterKey(), session, message, message.getAllRecipients());
6870
}
6971
} catch (final UnsupportedEncodingException e) {
70-
LOGGER.error("Failed to send email:\n{}", email.getId());
71-
LOGGER.trace("{}", email);
72-
throw new MailerException(MailerException.INVALID_ENCODING, e);
72+
handleException(e, INVALID_ENCODING);
7373
} catch (final MessagingException e) {
74-
LOGGER.error("Failed to send email:\n{}", email.getId());
75-
LOGGER.trace("{}", email);
76-
throw new MailerException(MailerException.GENERIC_ERROR, e);
74+
handleException(e, GENERIC_ERROR);
7775
} catch (final Exception e) {
78-
LOGGER.error("Failed to send email:\n{}", email.getId());
79-
LOGGER.trace("{}", email);
80-
throw e;
76+
handleException(e, UNKNOWN_ERROR);
8177
}
8278
}
8379

80+
private void handleException(final Exception e, String errorMsg) {
81+
LOGGER.trace("Failed to send email {}\n{}", email.getId(), email);
82+
throw new MailerException(format(errorMsg, email.getId()), e);
83+
}
84+
8485
private void logEmail(final MimeMessage message) {
8586
if (transportModeLoggingOnly) {
8687
if (LOGGER.isInfoEnabled()) {

0 commit comments

Comments
 (0)