From 040b33370bbbe572ae02e8d6e979ffbdb17ae340 Mon Sep 17 00:00:00 2001 From: Su Date: Thu, 23 Mar 2023 13:11:40 -0700 Subject: [PATCH] update logic Signed-off-by: Su --- .../routes/api/console/proxy/create_handler.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts index 537a526e181c..a0f8926e0030 100644 --- a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts +++ b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts @@ -30,6 +30,7 @@ import { OpenSearchDashboardsRequest, RequestHandler } from 'opensearch-dashboards/server'; import { trimStart } from 'lodash'; +import { Readable } from 'stream'; import { ApiResponse } from '@opensearch-project/opensearch/'; @@ -130,9 +131,23 @@ export const createHandler = ({ } catch (e: any) { log.error(e); const isResponseErrorFlag = isResponseError(e); + + const errorMessage = isResponseErrorFlag + ? JSON.stringify(e.meta.body) + : `502.${e.statusCode || 0}`; + // core http route handler has special logic that asks for stream readable input to pass error opaquely + const errorResponseBody = new Readable({ + read() { + this.push(errorMessage); + this.push(null); + }, + }); return response.customError({ statusCode: isResponseErrorFlag ? e.statusCode : 502, - body: isResponseErrorFlag ? JSON.stringify(e.meta.body) : `502.${e.statusCode || 0}`, + body: errorResponseBody, + headers: { + 'Content-Type': 'application/json', + }, }); } };