Skip to content

Commit

Permalink
Fix improper error handling in dev tools with MDS
Browse files Browse the repository at this point in the history
Signed-off-by: Zhongnan Su <szhongna@amazon.com>
  • Loading branch information
zhongnansu committed Oct 23, 2024
1 parent 9a25d0d commit b1c460f
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ export const createHandler = ({
}: RouteDependencies): RequestHandler<unknown, Query, Body> => async (ctx, request, response) => {
const { body, query } = request;
const { path, method, dataSourceId } = query;
const client = dataSourceId
? await ctx.dataSource.opensearch.getClient(dataSourceId)
: ctx.core.opensearch.client.asCurrentUserWithLongNumeralsSupport;
let opensearchResponse: ApiResponse;

if (!pathFilters.some((re) => re.test(path))) {
return response.forbidden({
Expand All @@ -103,6 +99,10 @@ export const createHandler = ({
}

try {
const client = dataSourceId
? await ctx.dataSource.opensearch.getClient(dataSourceId)
: ctx.core.opensearch.client.asCurrentUserWithLongNumeralsSupport;

// TODO: proxy header will fail sigv4 auth type in data source, need create issue in opensearch-js repo to track
const requestHeaders = dataSourceId
? {}
Expand All @@ -111,7 +111,7 @@ export const createHandler = ({
};

const bufferedBody = await buildBufferedBody(body);
opensearchResponse = await client.transport.request(
const opensearchResponse = await client.transport.request(
{ path: toUrlPath(path), method, body: bufferedBody },
{ headers: requestHeaders }
);
Expand Down Expand Up @@ -148,7 +148,7 @@ export const createHandler = ({
});
} catch (e: any) {
const isResponseErrorFlag = isResponseError(e);
if (!isResponseError) log.error(e);
if (!isResponseErrorFlag) log.error(e);
const errorMessage = isResponseErrorFlag ? stringify(e.meta.body) : e.message;
// core http route handler has special logic that asks for stream readable input to pass error opaquely
const errorResponseBody = new Readable({
Expand All @@ -158,7 +158,7 @@ export const createHandler = ({
},
});
return response.customError({
statusCode: isResponseErrorFlag ? e.statusCode : 502,
statusCode: e.statusCode || 502,
body: errorResponseBody,
headers: {
'Content-Type': 'application/json',
Expand Down

0 comments on commit b1c460f

Please sign in to comment.