Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] [Discover] fix error construction and retrieving status #8820

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading