Skip to content

Commit

Permalink
update unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Li <[email protected]>
  • Loading branch information
joshuali925 committed Nov 15, 2024
1 parent 896ca26 commit 742da55
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
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);
});
});
12 changes: 10 additions & 2 deletions 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 @@ -91,9 +100,8 @@ export function defineSearchStrategyRouteProvider(logger: Logger, router: IRoute
} catch (e) {
error = err;
}
const statusCode = error.status || err.status;
return res.custom({
statusCode: statusCode === 500 ? 503 : statusCode || 503,
statusCode: coerceStatusCode(error.status || err.status),
body: err.message,
});
}
Expand Down

0 comments on commit 742da55

Please sign in to comment.