legacy annotation query - handle undefined scenarios without crashing (#106275)

legacy annotation query - handle undefined scenario
This commit is contained in:
Scott Lepper
2025-06-03 08:33:36 -04:00
committed by GitHub
parent 7e271796c1
commit 8b262046e0
2 changed files with 38 additions and 3 deletions

View File

@ -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 = {

View File

@ -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));
}
}