Improved error message when -email-from is empty while sending mail

This commit is contained in:
ned3y2k 2022-03-24 22:02:12 +09:00
parent 360ae9e4b9
commit d6e674bcf6
2 changed files with 30 additions and 0 deletions

View file

@ -8,6 +8,26 @@ import (
mail "github.com/xhit/go-simple-mail/v2"
)
type SmtpMailErrorCode int
const (
EmptyEmailFrom SmtpMailErrorCode = 1 + iota
)
type SmtpMailError struct {
Msg string
Code SmtpMailErrorCode
}
func (err SmtpMailError) Error() string {
switch err.Code {
case EmptyEmailFrom:
return "-email-from is not specified or is empty.\nCheck the usage with --help."
default:
panic("Not Implemented")
}
}
type SmtpMail struct {
hostname string
port int
@ -65,6 +85,10 @@ func (o *SmtpMail) Send(toName string, to string, subject string, content string
return err
}
if o.from == "" {
return SmtpMailError{Msg: "", Code: EmptyEmailFrom}
}
email := mail.NewMSG()
email.SetFrom(addressField(o.from, o.fromName)).
AddTo(addressField(to, toName)).

View file

@ -275,6 +275,12 @@ func EmailClient(db store.IStore, mailer emailer.Emailer, emailSubject, emailCon
)
if err != nil {
_, isMailError := err.(emailer.SmtpMailError)
if isMailError {
fmt.Println("SmtpMailError", err)
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "An error occurred while sending mail. Contact your administrator."})
}
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, err.Error()})
}