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 <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
jgulick48
2020-10-20 06:53:48 -05:00
committed by GitHub
parent d13c6b4c5a
commit 5bc6c447e3
3 changed files with 8 additions and 2 deletions

View File

@@ -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)
}

View File

@@ -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

View File

@@ -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