From 8b262046e01cb16568b7fd61a0edd816532f6aaa Mon Sep 17 00:00:00 2001 From: Scott Lepper Date: Tue, 3 Jun 2025 08:33:36 -0400 Subject: [PATCH] legacy annotation query - handle undefined scenarios without crashing (#106275) legacy annotation query - handle undefined scenario --- .../LegacyAnnotationQueryRunner.test.ts | 26 +++++++++++++++++++ .../LegacyAnnotationQueryRunner.ts | 15 ++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.test.ts b/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.test.ts index 25c25318896..416099f72bc 100644 --- a/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.test.ts +++ b/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.test.ts @@ -48,6 +48,32 @@ describe('LegacyAnnotationQueryRunner', () => { }); }); + describe('when run is called without a valid datasource', () => { + it('then it should return empty results when datasource is undefined', async () => { + const datasource = undefined; + const options = { ...getDefaultOptions(), datasource }; + + await expect(runner.run(options)).toEmitValuesWith((received) => { + expect(received).toHaveLength(1); + const results = received[0]; + expect(results).toEqual([]); + }); + }); + + it('then it should return empty results when annotationQuery is undefined', async () => { + const datasource = { + annotationQuery: undefined, + } as unknown as DataSourceApi; + const options = { ...getDefaultOptions(), datasource }; + + await expect(runner.run(options)).toEmitValuesWith((received) => { + expect(received).toHaveLength(1); + const results = received[0]; + expect(results).toEqual([]); + }); + }); + }); + describe('when canWork is called with incorrect props', () => { it('then it should return false', () => { const datasource = { diff --git a/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.ts b/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.ts index f383897f5d9..be28bbbb533 100644 --- a/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.ts +++ b/public/app/features/query/state/DashboardQueryRunner/LegacyAnnotationQueryRunner.ts @@ -25,8 +25,17 @@ export class LegacyAnnotationQueryRunner implements AnnotationQueryRunner { return of([]); } - return from(datasource!.annotationQuery!({ range, rangeRaw: range.raw, annotation, dashboard })).pipe( - catchError(handleAnnotationQueryRunnerError) - ); + if (datasource?.annotationQuery === undefined) { + console.warn('datasource does not have an annotation query'); + return of([]); + } + + const annotationQuery = datasource.annotationQuery({ range, rangeRaw: range.raw, annotation, dashboard }); + if (annotationQuery === undefined) { + console.warn('datasource does not have an annotation query'); + return of([]); + } + + return from(annotationQuery).pipe(catchError(handleAnnotationQueryRunnerError)); } }