From bba85c112845f1bb0b0f62906898900ee743ac7e Mon Sep 17 00:00:00 2001 From: Stephanie Hingtgen Date: Fri, 11 Apr 2025 13:31:41 -0600 Subject: [PATCH] K8s: Dashboards: Fix error handling (#103929) --- pkg/registry/apis/dashboard/register.go | 16 ++++++++-------- .../dashboard/integration/api_validation_test.go | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/registry/apis/dashboard/register.go b/pkg/registry/apis/dashboard/register.go index 82a5ed934f1..51d0ab4ef43 100644 --- a/pkg/registry/apis/dashboard/register.go +++ b/pkg/registry/apis/dashboard/register.go @@ -254,12 +254,12 @@ func (b *DashboardsAPIBuilder) validateCreate(ctx context.Context, a admission.A // Basic validations if err := b.dashboardService.ValidateBasicDashboardProperties(title, accessor.GetName(), accessor.GetMessage()); err != nil { - return err + return apierrors.NewBadRequest(err.Error()) } // Validate refresh interval if err := b.dashboardService.ValidateDashboardRefreshInterval(b.cfg.MinRefreshInterval, refresh); err != nil { - return err + return apierrors.NewBadRequest(err.Error()) } id, err := identity.GetRequester(ctx) @@ -270,7 +270,7 @@ func (b *DashboardsAPIBuilder) validateCreate(ctx context.Context, a admission.A // Validate folder existence if specified if !a.IsDryRun() && accessor.GetFolder() != "" { if err := b.validateFolderExists(ctx, accessor.GetFolder(), id.GetOrgID()); err != nil { - return err + return apierrors.NewNotFound(folders.FolderResourceInfo.GroupResource(), accessor.GetFolder()) } } @@ -288,7 +288,7 @@ func (b *DashboardsAPIBuilder) validateCreate(ctx context.Context, a admission.A return err } if quotaReached { - return dashboards.ErrQuotaReached + return apierrors.NewForbidden(dashv1.DashboardResourceInfo.GroupResource(), a.GetName(), dashboards.ErrQuotaReached) } } @@ -324,19 +324,19 @@ func (b *DashboardsAPIBuilder) validateUpdate(ctx context.Context, a admission.A // Basic validations if err := b.dashboardService.ValidateBasicDashboardProperties(title, newAccessor.GetName(), newAccessor.GetMessage()); err != nil { - return err + return apierrors.NewBadRequest(err.Error()) } // Validate folder existence if specified and changed if !a.IsDryRun() && newAccessor.GetFolder() != "" && newAccessor.GetFolder() != oldAccessor.GetFolder() { if err := b.validateFolderExists(ctx, newAccessor.GetFolder(), nsInfo.OrgID); err != nil { - return err + return apierrors.NewNotFound(folders.FolderResourceInfo.GroupResource(), newAccessor.GetFolder()) } } // Validate refresh interval if err := b.dashboardService.ValidateDashboardRefreshInterval(b.cfg.MinRefreshInterval, refresh); err != nil { - return err + return apierrors.NewBadRequest(err.Error()) } allowOverwrite := false // TODO: Add support for overwrite flag @@ -345,7 +345,7 @@ func (b *DashboardsAPIBuilder) validateUpdate(ctx context.Context, a admission.A if allowOverwrite { newAccessor.SetGeneration(oldAccessor.GetGeneration()) } else { - return dashboards.ErrDashboardVersionMismatch + return apierrors.NewBadRequest(dashboards.ErrDashboardVersionMismatch.Error()) } } diff --git a/pkg/tests/apis/dashboard/integration/api_validation_test.go b/pkg/tests/apis/dashboard/integration/api_validation_test.go index 540498c91eb..fa5a3f2b0c6 100644 --- a/pkg/tests/apis/dashboard/integration/api_validation_test.go +++ b/pkg/tests/apis/dashboard/integration/api_validation_test.go @@ -212,7 +212,7 @@ func runDashboardValidationTests(t *testing.T, ctx TestContext) { t.Run("reject dashboard with non-existent folder UID", func(t *testing.T) { nonExistentFolderUID := "non-existent-folder-uid" _, err := createDashboard(t, adminClient, "Dashboard in Non-existent Folder", &nonExistentFolderUID, nil) - ctx.Helper.EnsureStatusError(err, http.StatusNotFound, "folder not found") + ctx.Helper.EnsureStatusError(err, http.StatusNotFound, "folders.folder.grafana.app \"non-existent-folder-uid\" not found") }) })