Commit 56dd7fcb by Chris Cowan Committed by GitHub

Elasticsearch: Display errors with text responses (#30122)

Co-authored-by: Elfo404 <gio.ricci@grafana.com>
parent 64254eaa
......@@ -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 },
......
......@@ -127,9 +127,11 @@ export class ElasticDatasource extends DataSourceApi<ElasticsearchQuery, Elastic
return results.data;
}),
catchError((err) => {
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,
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment