From 5bc6c447e3e7208bc3caf436f581f759b2b303ec Mon Sep 17 00:00:00 2001 From: jgulick48 Date: Tue, 20 Oct 2020 06:53:48 -0500 Subject: [PATCH] Alerting: Return proper status code when trying to create alert notification channel with duplicate name or uid (#28043) * Alerting: Return proper status code when trying to create an Alert Notification where the name or UID already exists. Co-authored-by: Arve Knudsen Signed-off-by: Arve Knudsen --- pkg/api/alerting.go | 4 ++++ pkg/models/alert_notifications.go | 2 ++ pkg/services/sqlstore/alert_notification.go | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 52ff0f65a82..d047bc7eb99 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -1,6 +1,7 @@ package api import ( + "errors" "fmt" "strconv" @@ -273,6 +274,9 @@ func CreateAlertNotification(c *models.ReqContext, cmd models.CreateAlertNotific cmd.OrgId = c.OrgId if err := bus.Dispatch(&cmd); err != nil { + if errors.Is(err, models.ErrAlertNotificationWithSameNameExists) || errors.Is(err, models.ErrAlertNotificationWithSameUIDExists) { + return Error(409, "Failed to create alert notification", err) + } return Error(500, "Failed to create alert notification", err) } diff --git a/pkg/models/alert_notifications.go b/pkg/models/alert_notifications.go index 2d0886e4d53..812be31e74b 100644 --- a/pkg/models/alert_notifications.go +++ b/pkg/models/alert_notifications.go @@ -15,6 +15,8 @@ var ( ErrAlertNotificationStateVersionConflict = errors.New("alert notification state update version conflict") ErrAlertNotificationStateAlreadyExist = errors.New("alert notification state already exists") ErrAlertNotificationFailedGenerateUniqueUid = errors.New("Failed to generate unique alert notification uid") + ErrAlertNotificationWithSameNameExists = errors.New("alert notification with same name already exists") + ErrAlertNotificationWithSameUIDExists = errors.New("alert notification with same uid already exists") ) type AlertNotificationStateType string diff --git a/pkg/services/sqlstore/alert_notification.go b/pkg/services/sqlstore/alert_notification.go index 548bddc1823..13ca2c32f1b 100644 --- a/pkg/services/sqlstore/alert_notification.go +++ b/pkg/services/sqlstore/alert_notification.go @@ -299,7 +299,7 @@ func CreateAlertNotificationCommand(cmd *models.CreateAlertNotificationCommand) } if existingQuery.Result != nil { - return fmt.Errorf("Alert notification uid %s already exists", cmd.Uid) + return models.ErrAlertNotificationWithSameUIDExists } // check if name exists @@ -309,7 +309,7 @@ func CreateAlertNotificationCommand(cmd *models.CreateAlertNotificationCommand) } if sameNameQuery.Result != nil { - return fmt.Errorf("Alert notification name %s already exists", cmd.Name) + return models.ErrAlertNotificationWithSameNameExists } var frequency time.Duration