diff --git a/server/routes/query_assist/routes.ts b/server/routes/query_assist/routes.ts index a3e5b0bedd..d34d5256db 100644 --- a/server/routes/query_assist/routes.ts +++ b/server/routes/query_assist/routes.ts @@ -83,11 +83,15 @@ export function registerQueryAssistRoutes(router: IRouter) { if ( isResponseError(error) && error.statusCode === 400 && - error.body.includes(ERROR_DETAILS.GUARDRAILS_TRIGGERED) + // on opensearch >= 2.17, error.body is an object https://github.com/opensearch-project/ml-commons/pull/2858 + JSON.stringify(error.body).includes(ERROR_DETAILS.GUARDRAILS_TRIGGERED) ) { return response.badRequest({ body: ERROR_DETAILS.GUARDRAILS_TRIGGERED }); } - return response.custom({ statusCode: error.statusCode || 500, body: error.message }); + return response.custom({ + statusCode: error.statusCode || 500, + body: typeof error.body === 'string' ? error.body : JSON.stringify(error.body), + }); } } ); @@ -155,11 +159,15 @@ export function registerQueryAssistRoutes(router: IRouter) { if ( isResponseError(error) && error.statusCode === 400 && - error.body.includes(ERROR_DETAILS.GUARDRAILS_TRIGGERED) + // on opensearch >= 2.17, error.body is an object https://github.com/opensearch-project/ml-commons/pull/2858 + JSON.stringify(error.body).includes(ERROR_DETAILS.GUARDRAILS_TRIGGERED) ) { return response.badRequest({ body: ERROR_DETAILS.GUARDRAILS_TRIGGERED }); } - return response.custom({ statusCode: error.statusCode || 500, body: error.message }); + return response.custom({ + statusCode: error.statusCode || 500, + body: typeof error.body === 'string' ? error.body : JSON.stringify(error.body), + }); } } );