Skip to content

Commit

Permalink
[Discover] fix error construction and retrieving status (#8807)
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Li <[email protected]>
  • Loading branch information
joshuali925 authored Nov 6, 2024
1 parent 4f6b287 commit 016f090
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/plugins/query_enhancements/common/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { handleFacetError } from './utils';

describe('handleFacetError', () => {
const error = new Error('mock-error');
(error as any).body = {
message: 'test error message',
};
(error as any).status = '400';
it('should throw an error with message from response.data.body.message', () => {
const response = {
data: error,
};

expect(() => handleFacetError(response)).toThrowError();
try {
handleFacetError(response);
} catch (err: any) {
expect(err.message).toBe('test error message');
expect(err.name).toBe('400');
expect(err.status).toBe('400');
}
});
});
2 changes: 1 addition & 1 deletion src/plugins/query_enhancements/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const removeKeyword = (queryString: string | undefined) => {
};

export const handleFacetError = (response: any) => {
const error = new Error(response.data.body ?? response.data);
const error = new Error(response.data.body?.message ?? response.data.body ?? response.data);
error.name = response.data.status ?? response.status ?? response.data.statusCode;
(error as any).status = error.name;
throw error;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/query_enhancements/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function defineSearchStrategyRouteProvider(logger: Logger, router: IRoute
error = err;
}
return res.custom({
statusCode: error.status,
statusCode: error.status || err.status,
body: err.message,
});
}
Expand Down

0 comments on commit 016f090

Please sign in to comment.