From 5b1498f98f3e1e386381d4e8e630cea75653c5a9 Mon Sep 17 00:00:00 2001 From: Alexander Weaver Date: Mon, 15 Apr 2024 09:25:28 -0500 Subject: [PATCH] Alerting: Return a 400 and errutil error when trying to delete a contact point that is referenced by a policy (#85481) Return a 400 and errutil error when trying to delete a contact point that is referenced by a policy --- pkg/services/ngalert/api/api_provisioning.go | 2 +- pkg/services/ngalert/provisioning/contactpoints.go | 2 +- pkg/services/ngalert/provisioning/errors.go | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/services/ngalert/api/api_provisioning.go b/pkg/services/ngalert/api/api_provisioning.go index 8143a6eafa4..9d0f062d2ad 100644 --- a/pkg/services/ngalert/api/api_provisioning.go +++ b/pkg/services/ngalert/api/api_provisioning.go @@ -192,7 +192,7 @@ func (srv *ProvisioningSrv) RoutePutContactPoint(c *contextmodel.ReqContext, cp func (srv *ProvisioningSrv) RouteDeleteContactPoint(c *contextmodel.ReqContext, UID string) response.Response { err := srv.contactPointService.DeleteContactPoint(c.Req.Context(), c.SignedInUser.GetOrgID(), UID) if err != nil { - return ErrResp(http.StatusInternalServerError, err, "") + return response.ErrOrFallback(http.StatusInternalServerError, "failed to delete contact point", err) } return response.JSON(http.StatusAccepted, util.DynMap{"message": "contactpoint deleted"}) } diff --git a/pkg/services/ngalert/provisioning/contactpoints.go b/pkg/services/ngalert/provisioning/contactpoints.go index 37a3c03925a..02eeb66978d 100644 --- a/pkg/services/ngalert/provisioning/contactpoints.go +++ b/pkg/services/ngalert/provisioning/contactpoints.go @@ -338,7 +338,7 @@ func (ecp *ContactPointService) DeleteContactPoint(ctx context.Context, orgID in } } if fullRemoval && isContactPointInUse(name, []*apimodels.Route{revision.cfg.AlertmanagerConfig.Route}) { - return fmt.Errorf("contact point '%s' is currently used by a notification policy", name) + return ErrContactPointReferenced } return ecp.xact.InTransaction(ctx, func(ctx context.Context) error { diff --git a/pkg/services/ngalert/provisioning/errors.go b/pkg/services/ngalert/provisioning/errors.go index 31e50224ffe..33ab23544e6 100644 --- a/pkg/services/ngalert/provisioning/errors.go +++ b/pkg/services/ngalert/provisioning/errors.go @@ -19,6 +19,8 @@ var ( ErrTimeIntervalExists = errutil.BadRequest("alerting.notifications.time-intervals.nameExists", errutil.WithPublicMessage("Time interval with this name already exists. Use a different name or update existing one.")) ErrTimeIntervalInvalid = errutil.BadRequest("alerting.notifications.time-intervals.invalidFormat").MustTemplate("Invalid format of the submitted time interval", errutil.WithPublic("Time interval is in invalid format. Correct the payload and try again.")) ErrTimeIntervalInUse = errutil.Conflict("alerting.notifications.time-intervals.used", errutil.WithPublicMessage("Time interval is used by one or many notification policies")) + + ErrContactPointReferenced = errutil.BadRequest("alerting.notifications.contact-points.referenced", errutil.WithPublicMessage("Contact point is currently referenced by a notification policy.")) ) func makeErrBadAlertmanagerConfiguration(err error) error {