Adding better error handling for SMTP server

JSBeautify sending_profiles.js
pull/284/head
Jordan Wright 2016-05-30 14:53:32 -05:00
parent 8211116375
commit 1933eb7ff1
3 changed files with 53 additions and 31 deletions

View File

@ -416,7 +416,7 @@ func API_SMTP_Id(w http.ResponseWriter, r *http.Request) {
} }
err = s.Validate() err = s.Validate()
if err != nil { if err != nil {
JSONResponse(w, models.Response{Success: false, Message: "Invalid attributes given"}, http.StatusBadRequest) JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusBadRequest)
return return
} }
s.ModifiedDate = time.Now() s.ModifiedDate = time.Now()

View File

@ -3,6 +3,8 @@ package models
import ( import (
"errors" "errors"
"net/mail" "net/mail"
"strconv"
"strings"
"time" "time"
) )
@ -28,6 +30,9 @@ var ErrFromAddressNotSpecified = errors.New("No From Address specified")
// in the SMTP configuration // in the SMTP configuration
var ErrHostNotSpecified = errors.New("No SMTP Host specified") var ErrHostNotSpecified = errors.New("No SMTP Host specified")
// ErrInvalidHost indicates that the SMTP server string is invalid
var ErrInvalidHost = errors.New("Invalid SMTP server address")
// TableName specifies the database tablename for Gorm to use // TableName specifies the database tablename for Gorm to use
func (s SMTP) TableName() string { func (s SMTP) TableName() string {
return "smtp" return "smtp"
@ -42,6 +47,20 @@ func (s *SMTP) Validate() error {
return ErrHostNotSpecified return ErrHostNotSpecified
} }
_, err := mail.ParseAddress(s.FromAddress) _, err := mail.ParseAddress(s.FromAddress)
if err != nil {
return err
}
// Make sure addr is in host:port format
hp := strings.Split(s.Host, ":")
if len(hp) > 2 {
return ErrInvalidHost
} else if len(hp) < 2 {
hp = append(hp, "25")
}
_, err = strconv.Atoi(hp[1])
if err != nil {
return ErrInvalidHost
}
return err return err
} }

View File

@ -51,6 +51,9 @@ function save(idx) {
load() load()
dismiss() dismiss()
}) })
.error(function(data) {
modalError(data.responseJSON.message)
})
} else { } else {
// Submit the profile // Submit the profile
api.SMTP.post(profile) api.SMTP.post(profile)