Skip to content

Commit

Permalink
add body to error object (#6496)
Browse files Browse the repository at this point in the history
Signed-off-by: Lu Yu <[email protected]>
  • Loading branch information
BionIT authored Apr 16, 2024
1 parent b619ccb commit 4339f4e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/plugins/data_source/server/lib/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' } },
});
});
});
6 changes: 6 additions & 0 deletions src/plugins/data_source/server/lib/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 4339f4e

Please sign in to comment.