mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Elasticsearch: Display errors with text responses (#30122)
Co-authored-by: Elfo404 <gio.ricci@grafana.com>
This commit is contained in:
parent
64254eaa82
commit
56dd7fcbb2
@ -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,
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user