Commit 5bc6c447 by jgulick48 Committed by GitHub

Alerting: Return proper status code when trying to create alert notification…

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>
parent d13c6b4c
package api package api
import ( import (
"errors"
"fmt" "fmt"
"strconv" "strconv"
...@@ -273,6 +274,9 @@ func CreateAlertNotification(c *models.ReqContext, cmd models.CreateAlertNotific ...@@ -273,6 +274,9 @@ func CreateAlertNotification(c *models.ReqContext, cmd models.CreateAlertNotific
cmd.OrgId = c.OrgId cmd.OrgId = c.OrgId
if err := bus.Dispatch(&cmd); err != nil { 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) return Error(500, "Failed to create alert notification", err)
} }
......
...@@ -15,6 +15,8 @@ var ( ...@@ -15,6 +15,8 @@ var (
ErrAlertNotificationStateVersionConflict = errors.New("alert notification state update version conflict") ErrAlertNotificationStateVersionConflict = errors.New("alert notification state update version conflict")
ErrAlertNotificationStateAlreadyExist = errors.New("alert notification state already exists") ErrAlertNotificationStateAlreadyExist = errors.New("alert notification state already exists")
ErrAlertNotificationFailedGenerateUniqueUid = errors.New("Failed to generate unique alert notification uid") 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 type AlertNotificationStateType string
......
...@@ -299,7 +299,7 @@ func CreateAlertNotificationCommand(cmd *models.CreateAlertNotificationCommand) ...@@ -299,7 +299,7 @@ func CreateAlertNotificationCommand(cmd *models.CreateAlertNotificationCommand)
} }
if existingQuery.Result != nil { if existingQuery.Result != nil {
return fmt.Errorf("Alert notification uid %s already exists", cmd.Uid) return models.ErrAlertNotificationWithSameUIDExists
} }
// check if name exists // check if name exists
...@@ -309,7 +309,7 @@ func CreateAlertNotificationCommand(cmd *models.CreateAlertNotificationCommand) ...@@ -309,7 +309,7 @@ func CreateAlertNotificationCommand(cmd *models.CreateAlertNotificationCommand)
} }
if sameNameQuery.Result != nil { if sameNameQuery.Result != nil {
return fmt.Errorf("Alert notification name %s already exists", cmd.Name) return models.ErrAlertNotificationWithSameNameExists
} }
var frequency time.Duration var frequency time.Duration
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment