mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user