Graphite: Handle invalid annotations (#32437)

* Handle invalid annotations

* Add a test for handling invalid tags response

* Unify test descriptions
This commit is contained in:
Piotr Jamróz
2021-03-31 16:21:30 +02:00
committed by GitHub
parent be0c29a46c
commit 027e886997
2 changed files with 16 additions and 3 deletions

View File

@@ -233,15 +233,14 @@ describe('graphiteDatasource', () => {
},
];
beforeEach(() => {
beforeEach(async () => {
fetchMock.mockImplementation((options: any) => {
return of(createFetchResponse(response));
});
ctx.ds.annotationQuery(options).then((data: any) => {
await ctx.ds.annotationQuery(options).then((data: any) => {
results = data;
});
// ctx.$rootScope.$apply();
});
it('should parse the tags string into an array', () => {
@@ -251,6 +250,16 @@ describe('graphiteDatasource', () => {
expect(results[0].tags[1]).toEqual('tag2');
});
});
it('and tags response is invalid', async () => {
fetchMock.mockImplementation((options: any) => {
return of(createFetchResponse('zzzzzzz'));
});
await ctx.ds.annotationQuery(options).then((data: any) => {
results = data;
});
expect(results).toEqual([]);
});
});
describe('when fetching Graphite function descriptions', () => {

View File

@@ -261,6 +261,10 @@ export class GraphiteDatasource extends DataSourceApi<GraphiteQuery, GraphiteOpt
const tags = this.templateSrv.replace(options.annotation.tags);
return this.events({ range: options.range, tags: tags }).then((results: any) => {
const list = [];
if (!_.isArray(results.data)) {
console.error(`Unable to get annotations from ${results.url}.`);
return [];
}
for (let i = 0; i < results.data.length; i++) {
const e = results.data[i];