From 56dd7fcbb29c0284850d1b2435dfa9140841038d Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Tue, 2 Feb 2021 05:21:24 -0700 Subject: [PATCH] Elasticsearch: Display errors with text responses (#30122) Co-authored-by: Elfo404 --- .../elasticsearch/datasource.test.ts | 31 +++++++++++++++++++ .../datasource/elasticsearch/datasource.ts | 6 ++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/elasticsearch/datasource.test.ts b/public/app/plugins/datasource/elasticsearch/datasource.test.ts index 304666b3cda..eb7c45ba586 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.test.ts +++ b/public/app/plugins/datasource/elasticsearch/datasource.test.ts @@ -345,6 +345,37 @@ describe('ElasticDatasource', function (this: any) { }); }); + it('should properly throw an error with just a message', async () => { + const response: FetchResponse = { + data: { + error: 'Bad Request', + message: 'Authentication to data source failed', + }, + status: 400, + url: 'http://localhost:3000/api/tsdb/query', + config: { url: 'http://localhost:3000/api/tsdb/query' }, + type: 'basic', + statusText: 'Bad Request', + redirected: false, + headers: ({} as unknown) as Headers, + ok: false, + }; + + const { ds } = getTestContext({ + mockImplementation: () => throwError(response), + }); + + const errObject = { + error: 'Bad Request', + message: 'Elasticsearch error: Authentication to data source failed', + }; + + await expect(ds.query(query)).toEmitValuesWith((received) => { + expect(received.length).toBe(1); + expect(received[0]).toEqual(errObject); + }); + }); + it('should properly throw an unknown error', async () => { const { ds } = getTestContext({ jsonData: { interval: 'Daily', esVersion: 7 }, diff --git a/public/app/plugins/datasource/elasticsearch/datasource.ts b/public/app/plugins/datasource/elasticsearch/datasource.ts index dc3c6a90a47..ad7fd5da027 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.ts +++ b/public/app/plugins/datasource/elasticsearch/datasource.ts @@ -127,9 +127,11 @@ export class ElasticDatasource extends DataSourceApi { - if (err.data && err.data.error) { + if (err.data) { + const message = err.data.error?.reason ?? err.data.message ?? 'Unknown error'; + return throwError({ - message: 'Elasticsearch error: ' + err.data.error.reason, + message: 'Elasticsearch error: ' + message, error: err.data.error, }); }