go-mail/senderror.go
Winni Neessen 47bff15de9
Introduction of a Msg error type as proposal for #90
This PR introduces the `SendError` type which implements the error interface.

A new `senderror` field has been added to the `Msg` as well, so introduce this type to it.

I've also added different error variables that indicate the different things that can go wrong during mail delivery. These variables can be checked for, for each `Msg` using the `errors.As` method

The `Error()` method of `SendError` will return a detailed error string on why the `Msg` could not be delivered.

Additionally, `HasSendError()` and `SendError()` methods have been added to `Msg`. While `HasSendError()` simply returns a bool in case a `Msg` failed during delivery, the `SendError()` will return the full `SendError` error interface.
2022-12-31 12:40:42 +01:00

68 lines
2.1 KiB
Go

package mail
import (
"errors"
"fmt"
"strings"
)
// List of SendError errors
var (
// ErrGetSender is returned if the Msg.GetSender method fails during a Client.Send
ErrGetSender = errors.New("getting sender address")
// ErrGetRcpts is returned if the Msg.GetRecipients method fails during a Client.Send
ErrGetRcpts = errors.New("getting recipient addresses")
// ErrSMTPMailFrom is returned if the Msg delivery failed when sending the MAIL FROM command
// to the sending SMTP server
ErrSMTPMailFrom = errors.New("sending SMTP MAIL FROM command")
// ErrSMTPRcptTo is returned if the Msg delivery failed when sending the RCPT TO command
// to the sending SMTP server
ErrSMTPRcptTo = errors.New("sending SMTP RCPT TO command")
// ErrSMTPData is returned if the Msg delivery failed when sending the DATA command
// to the sending SMTP server
ErrSMTPData = errors.New("sending SMTP DATA command")
// ErrSMTPDataClose is returned if the Msg delivery failed when trying to close the
// Client data writer
ErrSMTPDataClose = errors.New("closing SMTP DATA writer")
// ErrSMTPReset is returned if the Msg delivery failed when sending the RSET command
// to the sending SMTP server
ErrSMTPReset = errors.New("sending SMTP RESET command")
// ErrWriteContent is returned if the Msg delivery failed when sending Msg content
// to the Client writer
ErrWriteContent = errors.New("sending message content")
// ErrConnCheck is returned if the Msg delivery failed when checking if the SMTP
// server connection is still working
ErrConnCheck = errors.New("checking SMTP connection")
)
// SendError is an error wrapper for delivery errors of the Msg
type SendError struct {
Err error
details []error
rcpt []string
}
// Error implements the error interface for the SendError type
func (e SendError) Error() string {
var em strings.Builder
_, _ = fmt.Fprintf(&em, "client_send: %s", e.Err)
if len(e.details) > 0 {
for i := range e.details {
em.WriteString(fmt.Sprintf(", error_details: %s", e.details[i]))
}
}
if len(e.rcpt) > 0 {
for i := range e.rcpt {
em.WriteString(fmt.Sprintf(", rcpt: %s", e.rcpt[i]))
}
}
return em.String()
}