From 7bf5b395b653a80e3420a0d1e55ab6cacfdc35fb Mon Sep 17 00:00:00 2001 From: Lukas Siatka Date: Mon, 25 May 2020 17:56:16 +0200 Subject: [PATCH] Chore: fixes throwing errors on 200 response with influxdb datasource (#24848) * Chore: fixes throwing errors on 200 response with influxdb datasource * Chore: changes influxdb error prefix from error to influxdb error --- .../plugins/datasource/influxdb/datasource.ts | 9 +++++ .../influxdb/specs/datasource.test.ts | 40 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index 06cd07885ae..964d48d7826 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -322,6 +322,15 @@ export default class InfluxDatasource extends DataSourceApi { + if (result.data && result.data.results) { + const errors = result.data.results.filter((elem: any) => elem.error); + if (errors.length > 0) { + throw { + message: 'InfluxDB Error: ' + errors[0].error, + data: result.data, + }; + } + } return result.data; }, (err: any) => { diff --git a/public/app/plugins/datasource/influxdb/specs/datasource.test.ts b/public/app/plugins/datasource/influxdb/specs/datasource.test.ts index bcada3993e4..d600d8830b8 100644 --- a/public/app/plugins/datasource/influxdb/specs/datasource.test.ts +++ b/public/app/plugins/datasource/influxdb/specs/datasource.test.ts @@ -75,6 +75,46 @@ describe('InfluxDataSource', () => { }); }); + describe('When getting error on 200 after issuing a query', () => { + const queryOptions: any = { + range: { + from: '2018-01-01T00:00:00Z', + to: '2018-01-02T00:00:00Z', + }, + rangeRaw: { + from: '2018-01-01T00:00:00Z', + to: '2018-01-02T00:00:00Z', + }, + targets: [{}], + timezone: 'UTC', + scopedVars: { + interval: { text: '1m', value: '1m' }, + __interval: { text: '1m', value: '1m' }, + __interval_ms: { text: 60000, value: 60000 }, + }, + }; + + it('throws an error', async () => { + datasourceRequestMock.mockImplementation((req: any) => { + return Promise.resolve({ + data: { + results: [ + { + error: 'Query timeout', + }, + ], + }, + }); + }); + + try { + await ctx.ds.query(queryOptions); + } catch (err) { + expect(err.message).toBe('InfluxDB Error: Query timeout'); + } + }); + }); + describe('InfluxDataSource in POST query mode', () => { const ctx: any = { //@ts-ignore