From 4339f4e089b2fe0343883d07cb8b8df55760a334 Mon Sep 17 00:00:00 2001 From: Lu Yu Date: Tue, 16 Apr 2024 16:36:07 -0700 Subject: [PATCH] add body to error object (#6496) Signed-off-by: Lu Yu --- .../data_source/server/lib/error.test.ts | 23 +++++++++++++++++++ src/plugins/data_source/server/lib/error.ts | 6 +++++ 2 files changed, 29 insertions(+) diff --git a/src/plugins/data_source/server/lib/error.test.ts b/src/plugins/data_source/server/lib/error.test.ts index 2ead6fde7345..7dec9280a450 100644 --- a/src/plugins/data_source/server/lib/error.test.ts +++ b/src/plugins/data_source/server/lib/error.test.ts @@ -117,4 +117,27 @@ describe('CreateDataSourceError', () => { new DataSourceError(new Error('dummy'), 'Connection Failure', 400) ); }); + + it('create data source error should be casted to a 400 DataSourceError', () => { + const error = new ResponseError({ + statusCode: 401, + body: { + error: { + type: 'index_not_found_exception', + }, + }, + warnings: [], + headers: { + 'WWW-Authenticate': 'content', + }, + meta: {} as any, + }); + + const actual = new DataSourceError(error); + + expect(actual).toMatchObject({ + statusCode: 401, + body: { error: { type: 'index_not_found_exception' } }, + }); + }); }); diff --git a/src/plugins/data_source/server/lib/error.ts b/src/plugins/data_source/server/lib/error.ts index 9d1108e13e16..4b5c44153aae 100644 --- a/src/plugins/data_source/server/lib/error.ts +++ b/src/plugins/data_source/server/lib/error.ts @@ -11,6 +11,8 @@ import { OsdError } from '../../../opensearch_dashboards_utils/common'; export class DataSourceError extends OsdError { // must have statusCode to avoid route handler in search.ts to return 500 statusCode: number; + body: any; + constructor(error: any, context?: string, statusCode?: number) { let message: string; if (context) { @@ -23,6 +25,10 @@ export class DataSourceError extends OsdError { super('Data Source Error: ' + message); + if (error.body) { + this.body = error.body; + } + if (statusCode) { this.statusCode = statusCode; } else if (error.statusCode) {