From dbb4eb7e1277607325bb69d7aeedff19be1379db Mon Sep 17 00:00:00 2001 From: Dominik Prokop Date: Wed, 3 Dec 2025 16:28:14 +0100 Subject: [PATCH] v2 conversion error: Throw DashboardVersionError correctly to redirecr to v1 API (#114772) v2 conversion error: Throw DashboardVersionError correctly --- public/app/features/dashboard/api/utils.ts | 4 ++++ public/app/features/dashboard/api/v2.test.ts | 8 ++++---- public/app/features/dashboard/api/v2.ts | 12 +++--------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/public/app/features/dashboard/api/utils.ts b/public/app/features/dashboard/api/utils.ts index 688218de024..ab3e995fc34 100644 --- a/public/app/features/dashboard/api/utils.ts +++ b/public/app/features/dashboard/api/utils.ts @@ -13,6 +13,10 @@ export function isV2StoredVersion(version: string | undefined): boolean { return version === 'v2alpha1' || version === 'v2beta1'; } +export function isV0V1StoredVersion(version: string | undefined): boolean { + return version === 'v0alpha1' || version === 'v1alpha1' || version === 'v1beta1'; +} + export function getDashboardsApiVersion(responseFormat?: 'v1' | 'v2') { const isDashboardSceneEnabled = config.featureToggles.dashboardScene; const isKubernetesDashboardsEnabled = config.featureToggles.kubernetesDashboards; diff --git a/public/app/features/dashboard/api/v2.test.ts b/public/app/features/dashboard/api/v2.test.ts index 58242c07c10..8790d969aab 100644 --- a/public/app/features/dashboard/api/v2.test.ts +++ b/public/app/features/dashboard/api/v2.test.ts @@ -317,7 +317,7 @@ describe('v2 dashboard API', () => { }); describe('version error handling', () => { - it('should not throw DashboardVersionError for v0alpha1 conversion error and v2 spec', async () => { + it('should throw DashboardVersionError for v0alpha1 conversion error and v2 spec', async () => { const mockDashboardWithError = { ...mockDashboardDto, status: { @@ -332,7 +332,7 @@ describe('v2 dashboard API', () => { mockGet.mockResolvedValueOnce(mockDashboardWithError); const api = new K8sDashboardV2API(); - await expect(api.getDashboardDTO('test')).resolves.toBe(mockDashboardWithError); + await expect(api.getDashboardDTO('test')).rejects.toThrow('backend conversion not yet implemented'); }); it('should throw DashboardVersionError for v0alpha1 conversion error and v1 spec', async () => { @@ -358,7 +358,7 @@ describe('v2 dashboard API', () => { await expect(api.getDashboardDTO('test')).rejects.toThrow('backend conversion not yet implemented'); }); - it('should not throw DashboardVersionError for v1beta1 conversion error and v2 spec', async () => { + it('should throw DashboardVersionError for v1beta1 conversion error and v2 spec', async () => { const mockDashboardWithError = { ...mockDashboardDto, status: { @@ -373,7 +373,7 @@ describe('v2 dashboard API', () => { mockGet.mockResolvedValueOnce(mockDashboardWithError); const api = new K8sDashboardV2API(); - await expect(api.getDashboardDTO('test')).resolves.toBe(mockDashboardWithError); + await expect(api.getDashboardDTO('test')).rejects.toThrow('backend conversion not yet implemented'); }); it('should throw DashboardVersionError for v1beta1 conversion error and v1 spec', async () => { diff --git a/public/app/features/dashboard/api/v2.ts b/public/app/features/dashboard/api/v2.ts index 35c5e1a7a0d..e182543ce9c 100644 --- a/public/app/features/dashboard/api/v2.ts +++ b/public/app/features/dashboard/api/v2.ts @@ -24,7 +24,7 @@ import { DashboardDTO, SaveDashboardResponseDTO } from 'app/types/dashboard'; import { SaveDashboardCommand } from '../components/SaveDashboard/types'; import { DashboardAPI, DashboardVersionError, DashboardWithAccessInfo, ListDeletedDashboardsOptions } from './types'; -import { isDashboardV2Spec } from './utils'; +import { isV0V1StoredVersion } from './utils'; export const K8S_V2_DASHBOARD_API_CONFIG = { group: 'dashboard.grafana.app', @@ -44,16 +44,10 @@ export class K8sDashboardV2API async getDashboardDTO(uid: string) { try { const dashboard = await this.client.subresource>(uid, 'dto'); - // FOR /dto calls returning v2 spec we are ignoring the conversion status to avoid runtime errors caused by the status // being saved for v2 resources that's been client-side converted to v2 and then PUT to the API server. - if ( - !isDashboardV2Spec(dashboard.spec) && - dashboard.status?.conversion?.failed && - (dashboard.status.conversion.storedVersion === 'v1alpha1' || - dashboard.status.conversion.storedVersion === 'v1beta1' || - dashboard.status.conversion.storedVersion === 'v0alpha1') - ) { + // This could come as conversion error from v0 or v2 to V1. + if (dashboard.status?.conversion?.failed && isV0V1StoredVersion(dashboard.status.conversion.storedVersion)) { throw new DashboardVersionError(dashboard.status.conversion.storedVersion, dashboard.status.conversion.error); }