From 7940da4803d512f50211a1463921cf0e048d057b Mon Sep 17 00:00:00 2001 From: Jo Date: Mon, 30 Jun 2025 13:58:37 +0200 Subject: [PATCH] Fix organization deletion error messages (#107380) * Fix organization deletion error messages - Improve error message clarity when attempting to delete active organization - Fix incorrect 'Failed to update organization' message to 'Failed to delete organization' - Update comment to be more precise about the check being performed Fixes #92792 * Improve error handling in organization deletion service - Add contextual error message when dashboard deletion fails during org deletion - Include organization ID in error message for better debugging - Import fmt package for error formatting This helps administrators understand which specific organization failed during the deletion process when dashboard cleanup encounters issues. * Fix typo in unified resource access error message Change 'Namespace missmatch' to 'Namespace mismatch' for correct spelling. --- pkg/api/org.go | 6 +++--- pkg/services/org/orgimpl/org_delete_svc.go | 3 ++- pkg/storage/unified/resource/access.go | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pkg/api/org.go b/pkg/api/org.go index c649afe4b12..2e15640c278 100644 --- a/pkg/api/org.go +++ b/pkg/api/org.go @@ -292,16 +292,16 @@ func (hs *HTTPServer) DeleteOrgByID(c *contextmodel.ReqContext) response.Respons if err != nil { return response.Error(http.StatusBadRequest, "orgId is invalid", err) } - // before deleting an org, check if user does not belong to the current org + // before deleting an org, check if user is not active in the org if c.GetOrgID() == orgID { - return response.Error(http.StatusBadRequest, "Can not delete org for current user", nil) + return response.Error(http.StatusBadRequest, "Cannot delete your active organization. Please switch to a different organization first.", nil) } if err := hs.orgDeletionService.Delete(c.Req.Context(), &org.DeleteOrgCommand{ID: orgID}); err != nil { if errors.Is(err, org.ErrOrgNotFound) { return response.Error(http.StatusNotFound, "Failed to delete organization. ID not found", nil) } - return response.Error(http.StatusInternalServerError, "Failed to update organization", err) + return response.Error(http.StatusInternalServerError, "Failed to delete organization", err) } return response.Success("Organization deleted") } diff --git a/pkg/services/org/orgimpl/org_delete_svc.go b/pkg/services/org/orgimpl/org_delete_svc.go index 4ea185aaef5..97676e786c4 100644 --- a/pkg/services/org/orgimpl/org_delete_svc.go +++ b/pkg/services/org/orgimpl/org_delete_svc.go @@ -3,6 +3,7 @@ package orgimpl import ( "context" "errors" + "fmt" "github.com/grafana/grafana/pkg/apimachinery/identity" "github.com/grafana/grafana/pkg/infra/db" @@ -59,7 +60,7 @@ func (s *DeletionService) Delete(ctx context.Context, cmd *org.DeleteOrgCommand) ctx, _ = identity.WithServiceIdentity(ctx, cmd.ID) err = s.dashSvc.DeleteAllDashboards(ctx, cmd.ID) if err != nil { - return err + return fmt.Errorf("failed to delete dashboards for org %d: %w", cmd.ID, err) } return s.store.Delete(ctx, cmd) diff --git a/pkg/storage/unified/resource/access.go b/pkg/storage/unified/resource/access.go index 6a21706a58e..4a2ea89d7a0 100644 --- a/pkg/storage/unified/resource/access.go +++ b/pkg/storage/unified/resource/access.go @@ -136,7 +136,7 @@ func (c authzLimitedClient) Check(ctx context.Context, id claims.AuthInfo, req c if !claims.NamespaceMatches(id.GetNamespace(), req.Namespace) { span.SetAttributes(attribute.Bool("allowed", false)) - span.SetStatus(codes.Error, "Namespace missmatch") + span.SetStatus(codes.Error, "Namespace mismatch") span.RecordError(claims.ErrNamespaceMissmatch) return claims.CheckResponse{Allowed: false}, claims.ErrNamespaceMissmatch } @@ -185,7 +185,7 @@ func (c authzLimitedClient) Compile(ctx context.Context, id claims.AuthInfo, req } if !claims.NamespaceMatches(id.GetNamespace(), req.Namespace) { span.SetAttributes(attribute.Bool("allowed", false)) - span.SetStatus(codes.Error, "Namespace missmatch") + span.SetStatus(codes.Error, "Namespace mismatch") span.RecordError(claims.ErrNamespaceMissmatch) return nil, claims.ErrNamespaceMissmatch }