mirror of
https://github.com/wneessen/go-mail.git
synced 2024-09-19 02:42:47 +02:00
Winni Neessen
78df991399
Did a complete overhaul of the senderror.go. - the list of `errors.New()` has been replaced with constant itoa error reasons as `SendErrReason`. Instead, the `Error()` method now reports the corresponding error message based on the reason. - The `SendError` received a `Is()` method so that we can use `errors.Is()` for very specific error checking. I.e. we can check for `&SendErrors{Reason: ErrSMTPMailFrom, isTemp: true}`. This provides much more flexibility in the error checking capabilities - A `isTemp` field has been added to the `SendError` type, indicating whether the received error is temporary and can be retried or not. Accordingly, the `*Msg` now has a `SendErrorIsTemp()` method indicating the same. The decision is based on the first 3 characters returned from the SMTP server. If the error code is within the 4xx range, the error is seen as temporary - A test for the SendError type has been added
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
package mail
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
// TestSendError_Error tests the SendError and SendErrReason error handling methods
|
|
func TestSendError_Error(t *testing.T) {
|
|
tl := []struct {
|
|
n string
|
|
r SendErrReason
|
|
te bool
|
|
}{
|
|
{"ErrGetSender/temp", ErrGetSender, true},
|
|
{"ErrGetSender/perm", ErrGetSender, false},
|
|
{"ErrGetRcpts/temp", ErrGetRcpts, true},
|
|
{"ErrGetRcpts/perm", ErrGetRcpts, false},
|
|
{"ErrSMTPMailFrom/temp", ErrSMTPMailFrom, true},
|
|
{"ErrSMTPMailFrom/perm", ErrSMTPMailFrom, false},
|
|
{"ErrSMTPRcptTo/temp", ErrSMTPRcptTo, true},
|
|
{"ErrSMTPRcptTo/perm", ErrSMTPRcptTo, false},
|
|
{"ErrSMTPData/temp", ErrSMTPData, true},
|
|
{"ErrSMTPData/perm", ErrSMTPData, false},
|
|
{"ErrSMTPDataClose/temp", ErrSMTPDataClose, true},
|
|
{"ErrSMTPDataClose/perm", ErrSMTPDataClose, false},
|
|
{"ErrSMTPReset/temp", ErrSMTPReset, true},
|
|
{"ErrSMTPReset/perm", ErrSMTPReset, false},
|
|
{"ErrWriteContent/temp", ErrWriteContent, true},
|
|
{"ErrWriteContent/perm", ErrWriteContent, false},
|
|
{"ErrConnCheck/temp", ErrConnCheck, true},
|
|
{"ErrConnCheck/perm", ErrConnCheck, false},
|
|
{"ErrNoUnencoded/temp", ErrNoUnencoded, true},
|
|
{"ErrNoUnencoded/perm", ErrNoUnencoded, false},
|
|
{"Unknown/temp", 9999, true},
|
|
{"Unknown/perm", 9999, false},
|
|
}
|
|
|
|
for _, tt := range tl {
|
|
t.Run(tt.n, func(t *testing.T) {
|
|
if err := returnSendError(tt.r, tt.te); err != nil {
|
|
exp := &SendError{Reason: tt.r, isTemp: tt.te}
|
|
if !errors.Is(err, exp) {
|
|
t.Errorf("error mismatch, expected: %s (temp: %t), got: %s (temp: %t)", tt.r, tt.te,
|
|
exp.Error(), exp.isTemp)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// returnSendError is a helper method to retunr a SendError with a specific reason
|
|
func returnSendError(r SendErrReason, t bool) error {
|
|
return &SendError{Reason: r, isTemp: t}
|
|
}
|