mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 15:08:03 +08:00
legacy annotation query - handle undefined scenarios without crashing (#106275)
legacy annotation query - handle undefined scenario
This commit is contained in:
@ -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 = {
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user