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] [Query enhancements] use status 503 if search strategy throws 500 #9002

Merged
merged 1 commit into from
Dec 5, 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
21 changes: 21 additions & 0 deletions src/plugins/query_enhancements/server/routes/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { coerceStatusCode } from '.';

describe('coerceStatusCode', () => {
it('should return 503 when input is 500', () => {
expect(coerceStatusCode(500)).toBe(503);
});

it('should return the input status code when it is not 500', () => {
expect(coerceStatusCode(404)).toBe(404);
});

it('should return 503 when input is undefined or null', () => {
expect(coerceStatusCode((undefined as unknown) as number)).toBe(503);
expect(coerceStatusCode((null as unknown) as number)).toBe(503);
});
});
11 changes: 10 additions & 1 deletion src/plugins/query_enhancements/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ import { API } from '../../common';
import { registerQueryAssistRoutes } from './query_assist';
import { registerDataSourceConnectionsRoutes } from './data_source_connection';

/**
* Coerce status code to 503 for 500 errors from dependency services. Only use
* this function to handle errors throw by other services, and not from OSD.
*/
export const coerceStatusCode = (statusCode: number) => {
if (statusCode === 500) return 503;
return statusCode || 503;
};

/**
* @experimental
*
Expand Down Expand Up @@ -92,7 +101,7 @@ export function defineSearchStrategyRouteProvider(logger: Logger, router: IRoute
error = err;
}
return res.custom({
statusCode: error.status || err.status,
statusCode: coerceStatusCode(error.status || err.status),
body: err.message,
});
}
Expand Down
Loading