Parcourir la source

fix: better error handling / messsage when testing email notification when stmp is not configured, fixes #8093

Torkel Ödegaard il y a 8 ans
Parent
commit
87c978ebc9
3 fichiers modifiés avec 5 ajouts et 1 suppressions
  1. 3 0
      pkg/api/alerting.go
  2. 1 0
      pkg/models/notifications.go
  3. 1 1
      pkg/services/notifications/mailer.go

+ 3 - 0
pkg/api/alerting.go

@@ -255,6 +255,9 @@ func NotificationTest(c *middleware.Context, dto dtos.NotificationTestCommand) R
 	}
 	}
 
 
 	if err := bus.Dispatch(cmd); err != nil {
 	if err := bus.Dispatch(cmd); err != nil {
+		if err == models.ErrSmtpNotEnabled {
+			return ApiError(412, err.Error(), err)
+		}
 		return ApiError(500, "Failed to send alert notifications", err)
 		return ApiError(500, "Failed to send alert notifications", err)
 	}
 	}
 
 

+ 1 - 0
pkg/models/notifications.go

@@ -3,6 +3,7 @@ package models
 import "errors"
 import "errors"
 
 
 var ErrInvalidEmailCode = errors.New("Invalid or expired email code")
 var ErrInvalidEmailCode = errors.New("Invalid or expired email code")
+var ErrSmtpNotEnabled = errors.New("SMTP not configured, check your grafana.ini config file's [smtp] section.")
 
 
 type SendEmailCommand struct {
 type SendEmailCommand struct {
 	To           []string
 	To           []string

+ 1 - 1
pkg/services/notifications/mailer.go

@@ -107,7 +107,7 @@ func createDialer() (*gomail.Dialer, error) {
 
 
 func buildEmailMessage(cmd *m.SendEmailCommand) (*Message, error) {
 func buildEmailMessage(cmd *m.SendEmailCommand) (*Message, error) {
 	if !setting.Smtp.Enabled {
 	if !setting.Smtp.Enabled {
-		return nil, errors.New("Grafana mailing/smtp options not configured, contact your Grafana admin")
+		return nil, m.ErrSmtpNotEnabled
 	}
 	}
 
 
 	var buffer bytes.Buffer
 	var buffer bytes.Buffer