mail: Rename RecoverableSMTPError to BadAddressSMTPError (#5479)

Rename `RecoverableSMTPError` to `BadAddressSMTPError`. The former
implies that an operation resulting in this error can be retried.
This commit is contained in:
Samantha 2021-06-15 11:04:56 -07:00 committed by GitHub
parent 205223abbc
commit 401d862354
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 13 deletions

View File

@ -170,9 +170,9 @@ func (m *mailer) run() error {
err := m.mailer.SendMail([]string{address}, m.subject, messageBody.String())
if err != nil {
var recoverableSMTPErr bmail.RecoverableSMTPError
if errors.As(err, &recoverableSMTPErr) {
m.log.Errf("Address %q was rejected by the server due to: %s", address, err)
var badAddrErr bmail.BadAddressSMTPError
if errors.As(err, &badAddrErr) {
m.log.Errf("address %q was rejected by server: %s", address, err)
continue
}
return fmt.Errorf("while sending mail (%d) of (%d) to address %q: %s",

View File

@ -306,22 +306,22 @@ func (m *MailerImpl) sendOne(to []string, subject, msg string) error {
return nil
}
// RecoverableSMTPError is returned by SendMail when the server rejects a message
// BadAddressSMTPError is returned by SendMail when the server rejects a message
// but for a reason that doesn't prevent us from continuing to send mail. The
// error message contains the error code and the error message returned from the
// server.
type RecoverableSMTPError struct {
type BadAddressSMTPError struct {
Message string
}
func (e RecoverableSMTPError) Error() string {
func (e BadAddressSMTPError) Error() string {
return e.Message
}
// Based on reading of various SMTP documents these are a handful
// of errors we are likely to be able to continue sending mail after
// receiving. The majority of these errors boil down to 'bad address'.
var recoverableErrorCodes = map[int]bool{
var badAddressErrorCodes = map[int]bool{
401: true, // Invalid recipient
422: true, // Recipient mailbox is full
441: true, // Recipient server is not responding
@ -377,9 +377,9 @@ func (m *MailerImpl) SendMail(to []string, subject, msg string) error {
m.reconnect()
// After reconnecting, loop around and try `sendOne` again.
continue
} else if errors.As(err, &protoErr) && recoverableErrorCodes[protoErr.Code] {
} else if errors.As(err, &protoErr) && badAddressErrorCodes[protoErr.Code] {
m.sendMailAttempts.WithLabelValues("failure", fmt.Sprintf("SMTP %d", protoErr.Code)).Inc()
return RecoverableSMTPError{fmt.Sprintf("%d: %s", protoErr.Code, protoErr.Msg)}
return BadAddressSMTPError{fmt.Sprintf("%d: %s", protoErr.Code, protoErr.Msg)}
} else {
// If it wasn't an EOF error or a recoverable SMTP error it is unexpected and we
// return from SendMail() with the error

View File

@ -359,12 +359,12 @@ func TestBadEmailError(t *testing.T) {
err = m.SendMail([]string{"hi@bye.com"}, "You are already a winner!", "Just kidding")
// We expect there to be an error
if err == nil {
t.Errorf("Expected SendMail() to return an RecoverableSMTPError, got nil")
t.Errorf("Expected SendMail() to return an BadAddressSMTPError, got nil")
}
expected := "401: 4.1.3 Bad recipient address syntax"
var rcptErr RecoverableSMTPError
test.AssertErrorWraps(t, err, &rcptErr)
test.AssertEquals(t, rcptErr.Message, expected)
var badAddrErr BadAddressSMTPError
test.AssertErrorWraps(t, err, &badAddrErr)
test.AssertEquals(t, badAddrErr.Message, expected)
}
func TestReconnectSMTP421(t *testing.T) {