From cfc975c893c1753df30b67522b05da0f0cedb03b Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Fri, 27 Dec 2024 14:07:30 +0800 Subject: [PATCH] Fix returns 500 error for AI agent APIs when OpenSearch returns 4xx error with json format error message (#380) (#381) * Fix returns 500 error for AI agent APIs when OpenSearch returns 4xx code with json format error message Signed-off-by: gaobinlong * Modify change log Signed-off-by: gaobinlong --------- Signed-off-by: gaobinlong (cherry picked from commit ffa509b4a125ed1e7e12303372b31e60c886deb3) Signed-off-by: github-actions[bot] # Conflicts: # CHANGELOG.md Co-authored-by: github-actions[bot] --- server/routes/agent_routes.test.ts | 135 +++++++++++ server/routes/agent_routes.ts | 4 +- server/routes/summary_routes.test.ts | 322 ++++++++++++++++++++++++++ server/routes/summary_routes.ts | 13 +- server/routes/text2viz_routes.test.ts | 238 +++++++++++++++++++ server/routes/text2viz_routes.ts | 8 +- 6 files changed, 701 insertions(+), 19 deletions(-) create mode 100644 server/routes/agent_routes.test.ts create mode 100644 server/routes/summary_routes.test.ts create mode 100644 server/routes/text2viz_routes.test.ts diff --git a/server/routes/agent_routes.test.ts b/server/routes/agent_routes.test.ts new file mode 100644 index 00000000..ad570c4c --- /dev/null +++ b/server/routes/agent_routes.test.ts @@ -0,0 +1,135 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { Boom } from '@hapi/boom'; +import { OpenSearchDashboardsRequest, Router } from '../../../../src/core/server/http/router'; +import { enhanceWithContext, triggerHandler } from './router.mock'; +import { resetMocks } from '../services/storage/agent_framework_storage_service.mock'; +import { httpServerMock } from '../../../../src/core/server/http/http_server.mocks'; +import { loggerMock } from '../../../../src/core/server/logging/logger.mock'; +import { AGENT_API } from '../../common/constants/llm'; +import { AssistantClient } from '../services/assistant_client'; +import { RequestHandlerContext } from '../../../../src/core/server'; +import { registerAgentRoutes } from './agent_routes'; +const mockedLogger = loggerMock.create(); + +export const createMockedAssistantClient = ( + request: OpenSearchDashboardsRequest +): AssistantClient => { + return new AssistantClient(request, {} as RequestHandlerContext); +}; + +const mockedAssistantClient = createMockedAssistantClient({} as OpenSearchDashboardsRequest); + +describe('test execute agent route', () => { + const router = new Router( + '', + mockedLogger, + enhanceWithContext({ + assistant_plugin: { + logger: mockedLogger, + }, + }) + ); + registerAgentRoutes(router, { + getScopedClient: jest.fn( + (request: OpenSearchDashboardsRequest, context: RequestHandlerContext) => { + return mockedAssistantClient; + } + ), + }); + const executeAgentRequest = (payload: {}, query: {}) => + triggerHandler(router, { + method: 'post', + path: AGENT_API.EXECUTE, + req: httpServerMock.createRawRequest({ + payload: JSON.stringify(payload), + query, + }), + }); + beforeEach(() => { + loggerMock.clear(mockedLogger); + resetMocks(); + }); + + it('return 4xx when execute agent throws 4xx error', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 429, + body: { + status: 429, + error: { + type: 'OpenSearchStatusException', + reason: 'System Error', + details: 'Request is throttled at model level.', + }, + }, + }); + const result = (await executeAgentRequest( + {}, + { + agentConfigName: 'os_insight', + } + )) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "{\\"status\\":429,\\"error\\":{\\"type\\":\\"OpenSearchStatusException\\",\\"reason\\":\\"System Error\\",\\"details\\":\\"Request is throttled at model level.\\"}}", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 4xx when executeAgent throws 4xx error in string format', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 429, + body: 'Request is throttled at model level', + }); + const result = (await executeAgentRequest( + {}, + { + agentConfigName: 'os_insight', + } + )) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "Request is throttled at model level", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 5xx when executeAgent throws 5xx error', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 500, + body: 'Server error', + }); + const result = (await executeAgentRequest( + {}, + { + agentConfigName: 'os_insight', + } + )) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Internal Server Error", + "message": "Execute agent failed!", + "statusCode": 500, + }, + "statusCode": 500, + } + `); + }); +}); diff --git a/server/routes/agent_routes.ts b/server/routes/agent_routes.ts index c8a9f3eb..826d740d 100644 --- a/server/routes/agent_routes.ts +++ b/server/routes/agent_routes.ts @@ -42,15 +42,13 @@ export function registerAgentRoutes(router: IRouter, assistantService: Assistant context.assistant_plugin.logger.error('Execute agent failed!', e); if (e.statusCode >= 400 && e.statusCode <= 499) { return res.customError({ - body: e.body, + body: { message: typeof e.body === 'string' ? e.body : JSON.stringify(e.body) }, statusCode: e.statusCode, - headers: e.headers, }); } else { return res.customError({ body: 'Execute agent failed!', statusCode: 500, - headers: e.headers, }); } } diff --git a/server/routes/summary_routes.test.ts b/server/routes/summary_routes.test.ts new file mode 100644 index 00000000..8033bc8b --- /dev/null +++ b/server/routes/summary_routes.test.ts @@ -0,0 +1,322 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { Boom } from '@hapi/boom'; +import { OpenSearchDashboardsRequest, Router } from '../../../../src/core/server/http/router'; +import { enhanceWithContext, triggerHandler } from './router.mock'; +import { resetMocks } from '../services/storage/agent_framework_storage_service.mock'; +import { httpServerMock } from '../../../../src/core/server/http/http_server.mocks'; +import { loggerMock } from '../../../../src/core/server/logging/logger.mock'; +import { SUMMARY_ASSISTANT_API } from '../../common/constants/llm'; +import { registerData2SummaryRoutes, registerSummaryAssistantRoutes } from './summary_routes'; +import { AssistantClient } from '../services/assistant_client'; +import { RequestHandlerContext } from '../../../../src/core/server'; +const mockedLogger = loggerMock.create(); + +export const createMockedAssistantClient = ( + request: OpenSearchDashboardsRequest +): AssistantClient => { + return new AssistantClient(request, {} as RequestHandlerContext); +}; + +const mockedAssistantClient = createMockedAssistantClient({} as OpenSearchDashboardsRequest); + +describe('test summary route', () => { + const router = new Router( + '', + mockedLogger, + enhanceWithContext({ + assistant_plugin: { + logger: mockedLogger, + }, + }) + ); + registerSummaryAssistantRoutes(router, { + getScopedClient: jest.fn( + (request: OpenSearchDashboardsRequest, context: RequestHandlerContext) => { + return mockedAssistantClient; + } + ), + }); + registerData2SummaryRoutes(router, { + getScopedClient: jest.fn( + (request: OpenSearchDashboardsRequest, context: RequestHandlerContext) => { + return mockedAssistantClient; + } + ), + }); + const summaryRequest = (payload: {}) => + triggerHandler(router, { + method: 'post', + path: SUMMARY_ASSISTANT_API.SUMMARIZE, + req: httpServerMock.createRawRequest({ + payload: JSON.stringify(payload), + }), + }); + const insightRequest = (payload: {}) => + triggerHandler(router, { + method: 'post', + path: SUMMARY_ASSISTANT_API.INSIGHT, + req: httpServerMock.createRawRequest({ + payload: JSON.stringify(payload), + }), + }); + const dataToSummaryRequest = (payload: {}) => + triggerHandler(router, { + method: 'post', + path: SUMMARY_ASSISTANT_API.DATA2SUMMARY, + req: httpServerMock.createRawRequest({ + payload: JSON.stringify(payload), + }), + }); + beforeEach(() => { + loggerMock.clear(mockedLogger); + resetMocks(); + }); + + it('return 4xx when execute agent throws 4xx error for summary API', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 429, + body: { + status: 429, + error: { + type: 'OpenSearchStatusException', + reason: 'System Error', + details: 'Request is throttled at model level.', + }, + }, + }); + const result = (await summaryRequest({ + summaryType: 'alerts', + insightType: 'user_insight', + question: 'Please summarize this alert, do not use any tool.', + context: 'context', + })) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "{\\"status\\":429,\\"error\\":{\\"type\\":\\"OpenSearchStatusException\\",\\"reason\\":\\"System Error\\",\\"details\\":\\"Request is throttled at model level.\\"}}", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 4xx when executeAgent throws 4xx error in string format for summary API', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 429, + body: 'Request is throttled at model level', + }); + const result = (await summaryRequest({ + summaryType: 'alerts', + insightType: 'user_insight', + question: 'Please summarize this alert, do not use any tool.', + context: 'context', + })) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "Request is throttled at model level", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 5xx when executeAgent throws 5xx error for summary API', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 500, + body: 'Server error', + }); + const result = (await summaryRequest({ + summaryType: 'alerts', + insightType: 'user_insight', + question: 'Please summarize this alert, do not use any tool.', + context: 'context', + })) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Internal Server Error", + "message": "Execute agent failed!", + "statusCode": 500, + }, + "statusCode": 500, + } + `); + }); + + it('return 4xx when execute agent throws 4xx error for insight API', async () => { + mockedAssistantClient.executeAgent = jest.fn().mockRejectedValue({ + statusCode: 429, + body: { + status: 429, + error: { + type: 'OpenSearchStatusException', + reason: 'System Error', + details: 'Request is throttled at model level.', + }, + }, + }); + const result = (await insightRequest({ + summaryType: 'alerts', + insightType: 'test', + summary: 'summary', + question: 'Please summarize this alert, do not use any tool.', + context: 'context', + })) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "{\\"status\\":429,\\"error\\":{\\"type\\":\\"OpenSearchStatusException\\",\\"reason\\":\\"System Error\\",\\"details\\":\\"Request is throttled at model level.\\"}}", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 4xx when executeAgent throws 4xx error in string format for insight API', async () => { + mockedAssistantClient.executeAgent = jest.fn().mockRejectedValue({ + statusCode: 429, + body: 'Request is throttled at model level', + }); + const result = (await insightRequest({ + summaryType: 'alerts', + insightType: 'test', + summary: 'summary', + question: 'Please summarize this alert, do not use any tool.', + context: 'context', + })) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "Request is throttled at model level", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 5xx when executeAgent throws 5xx for insight API', async () => { + mockedAssistantClient.executeAgent = jest.fn().mockRejectedValue({ + statusCode: 500, + body: 'Server error', + }); + const result = (await insightRequest({ + summaryType: 'alerts', + insightType: 'test', + summary: 'summary', + question: 'Please summarize this alert, do not use any tool.', + context: 'context', + })) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Internal Server Error", + "message": "Execute agent failed!", + "statusCode": 500, + }, + "statusCode": 500, + } + `); + }); + + it('return 4xx when execute agent throws 4xx error for data2Summary API', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 429, + body: { + status: 429, + error: { + type: 'OpenSearchStatusException', + reason: 'System Error', + details: 'Request is throttled at model level.', + }, + }, + }); + + const result = (await dataToSummaryRequest({ + sample_data: '223.87.60.27 - - [2018-07-22T00:39:02.912Z', + sample_count: 1, + total_count: 1, + question: 'Are there any errors in my logs?', + ppl: 'source=opensearch_dashboards_sample_data_logs| head 1', + })) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "{\\"status\\":429,\\"error\\":{\\"type\\":\\"OpenSearchStatusException\\",\\"reason\\":\\"System Error\\",\\"details\\":\\"Request is throttled at model level.\\"}}", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 4xx when executeAgent throws 4xx error in string format for data2Summary API', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 429, + body: 'Request is throttled at model level', + }); + const result = (await dataToSummaryRequest({ + sample_data: '223.87.60.27 - - [2018-07-22T00:39:02.912Z', + sample_count: 1, + total_count: 1, + question: 'Are there any errors in my logs?', + ppl: 'source=opensearch_dashboards_sample_data_logs| head 1', + })) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "Request is throttled at model level", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 5xx when executeAgent throws 5xx error for data2Summary API', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 500, + body: 'Server error', + }); + const result = (await dataToSummaryRequest({ + sample_data: '223.87.60.27 - - [2018-07-22T00:39:02.912Z', + sample_count: 1, + total_count: 1, + question: 'Are there any errors in my logs?', + ppl: 'source=opensearch_dashboards_sample_data_logs| head 1', + })) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Internal Server Error", + "message": "Execute agent failed!", + "statusCode": 500, + }, + "statusCode": 500, + } + `); + }); +}); diff --git a/server/routes/summary_routes.ts b/server/routes/summary_routes.ts index 79907f27..9d4aa9c6 100644 --- a/server/routes/summary_routes.ts +++ b/server/routes/summary_routes.ts @@ -47,7 +47,6 @@ export function registerSummaryAssistantRoutes( req.body.index && req.body.dsl && req.body.topNLogPatternData ? LOG_PATTERN_SUMMARY_AGENT_CONFIG_ID : SUMMARY_AGENT_CONFIG_ID; - let response; try { response = await assistantClient.executeAgentByConfigName(agentConfigId, { @@ -61,15 +60,13 @@ export function registerSummaryAssistantRoutes( context.assistant_plugin.logger.error('Execute agent failed!', e); if (e.statusCode >= 400 && e.statusCode <= 499) { return res.customError({ - body: e.body, + body: { message: typeof e.body === 'string' ? e.body : JSON.stringify(e.body) }, statusCode: e.statusCode, - headers: e.headers, }); } else { return res.customError({ body: 'Execute agent failed!', statusCode: 500, - headers: e.headers, }); } } @@ -150,15 +147,13 @@ export function registerSummaryAssistantRoutes( context.assistant_plugin.logger.error('Execute agent failed!', e); if (e.statusCode >= 400 && e.statusCode <= 499) { return res.customError({ - body: e.body, + body: { message: typeof e.body === 'string' ? e.body : JSON.stringify(e.body) }, statusCode: e.statusCode, - headers: e.headers, }); } else { return res.customError({ body: 'Execute agent failed!', statusCode: 500, - headers: e.headers, }); } } @@ -228,15 +223,13 @@ export function registerData2SummaryRoutes( context.assistant_plugin.logger.error('Execute agent failed!', e); if (e.statusCode >= 400 && e.statusCode <= 499) { return res.customError({ - body: e.body, + body: { message: typeof e.body === 'string' ? e.body : JSON.stringify(e.body) }, statusCode: e.statusCode, - headers: e.headers, }); } else { return res.customError({ body: 'Execute agent failed!', statusCode: 500, - headers: e.headers, }); } } diff --git a/server/routes/text2viz_routes.test.ts b/server/routes/text2viz_routes.test.ts new file mode 100644 index 00000000..946dd578 --- /dev/null +++ b/server/routes/text2viz_routes.test.ts @@ -0,0 +1,238 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { Boom } from '@hapi/boom'; +import { OpenSearchDashboardsRequest, Router } from '../../../../src/core/server/http/router'; +import { enhanceWithContext, triggerHandler } from './router.mock'; +import { resetMocks } from '../services/storage/agent_framework_storage_service.mock'; +import { httpServerMock } from '../../../../src/core/server/http/http_server.mocks'; +import { loggerMock } from '../../../../src/core/server/logging/logger.mock'; +import { TEXT2VIZ_API } from '../../common/constants/llm'; +import { AssistantClient } from '../services/assistant_client'; +import { RequestHandlerContext } from '../../../../src/core/server'; +import { registerText2VizRoutes } from './text2viz_routes'; +const mockedLogger = loggerMock.create(); + +export const createMockedAssistantClient = ( + request: OpenSearchDashboardsRequest +): AssistantClient => { + return new AssistantClient(request, {} as RequestHandlerContext); +}; + +const mockedAssistantClient = createMockedAssistantClient({} as OpenSearchDashboardsRequest); + +describe('test text2viz route', () => { + const router = new Router( + '', + mockedLogger, + enhanceWithContext({ + assistant_plugin: { + logger: mockedLogger, + }, + }) + ); + registerText2VizRoutes(router, { + getScopedClient: jest.fn( + (request: OpenSearchDashboardsRequest, context: RequestHandlerContext) => { + return mockedAssistantClient; + } + ), + }); + const text2vizRequest = (payload: {}, query: {}) => + triggerHandler(router, { + method: 'post', + path: TEXT2VIZ_API.TEXT2VEGA, + req: httpServerMock.createRawRequest({ + payload: JSON.stringify(payload), + query, + }), + }); + const text2pplRequest = (payload: {}, query: {}) => + triggerHandler(router, { + method: 'post', + path: TEXT2VIZ_API.TEXT2PPL, + req: httpServerMock.createRawRequest({ + payload: JSON.stringify(payload), + query, + }), + }); + beforeEach(() => { + loggerMock.clear(mockedLogger); + resetMocks(); + }); + + it('return 4xx when execute agent throws 4xx error for text2viz', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 429, + body: { + status: 429, + error: { + type: 'OpenSearchStatusException', + reason: 'System Error', + details: 'Request is throttled at model level.', + }, + }, + }); + const result = (await text2vizRequest( + { + input_question: 'question', + input_instruction: 'instruction', + ppl: 'ppl', + dataSchema: 'mapping', + sampleData: 'sample', + }, + {} + )) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "{\\"status\\":429,\\"error\\":{\\"type\\":\\"OpenSearchStatusException\\",\\"reason\\":\\"System Error\\",\\"details\\":\\"Request is throttled at model level.\\"}}", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 4xx when executeAgent throws 4xx error in string format for text2viz', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 429, + body: 'Request is throttled at model level', + }); + const result = (await text2vizRequest( + { + input_question: 'question', + input_instruction: 'instruction', + ppl: 'ppl', + dataSchema: 'mapping', + sampleData: 'sample', + }, + {} + )) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "Request is throttled at model level", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 5xx when executeAgent throws 5xx error for text2viz', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 500, + body: 'Server error', + }); + const result = (await text2vizRequest( + { + input_question: 'question', + input_instruction: 'instruction', + ppl: 'ppl', + dataSchema: 'mapping', + sampleData: 'sample', + }, + {} + )) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Internal Server Error", + "message": "Execute agent failed!", + "statusCode": 500, + }, + "statusCode": 500, + } + `); + }); + + it('return 4xx when execute agent throws 4xx error for text2ppl', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 429, + body: { + status: 429, + error: { + type: 'OpenSearchStatusException', + reason: 'System Error', + details: 'Request is throttled at model level.', + }, + }, + }); + const result = (await text2pplRequest( + { + index: 'index', + question: 'question', + }, + {} + )) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "{\\"status\\":429,\\"error\\":{\\"type\\":\\"OpenSearchStatusException\\",\\"reason\\":\\"System Error\\",\\"details\\":\\"Request is throttled at model level.\\"}}", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 4xx when executeAgent throws 4xx error in string format for text2ppl', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 429, + body: 'Request is throttled at model level', + }); + const result = (await text2pplRequest( + { + index: 'index', + question: 'question', + }, + {} + )) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Too Many Requests", + "message": "Request is throttled at model level", + "statusCode": 429, + }, + "statusCode": 429, + } + `); + }); + + it('return 5xx when executeAgent throws 5xx error for text2ppl', async () => { + mockedAssistantClient.executeAgentByConfigName = jest.fn().mockRejectedValue({ + statusCode: 500, + body: 'Server error', + }); + const result = (await text2pplRequest( + { + index: 'index', + question: 'question', + }, + {} + )) as Boom; + expect(result.output).toMatchInlineSnapshot(` + Object { + "headers": Object {}, + "payload": Object { + "error": "Internal Server Error", + "message": "Execute agent failed!", + "statusCode": 500, + }, + "statusCode": 500, + } + `); + }); +}); diff --git a/server/routes/text2viz_routes.ts b/server/routes/text2viz_routes.ts index 6db25c55..fb32e9b8 100644 --- a/server/routes/text2viz_routes.ts +++ b/server/routes/text2viz_routes.ts @@ -89,15 +89,13 @@ export function registerText2VizRoutes(router: IRouter, assistantService: Assist context.assistant_plugin.logger.error('Execute agent failed!', e); if (e.statusCode >= 400 && e.statusCode <= 499) { return res.customError({ - body: e.body, + body: { message: typeof e.body === 'string' ? e.body : JSON.stringify(e.body) }, statusCode: e.statusCode, - headers: e.headers, }); } else { return res.customError({ body: 'Execute agent failed!', statusCode: 500, - headers: e.headers, }); } } @@ -131,15 +129,13 @@ export function registerText2VizRoutes(router: IRouter, assistantService: Assist context.assistant_plugin.logger.error('Execute agent failed!', e); if (e.statusCode >= 400 && e.statusCode <= 499) { return res.customError({ - body: e.body, + body: { message: typeof e.body === 'string' ? e.body : JSON.stringify(e.body) }, statusCode: e.statusCode, - headers: e.headers, }); } else { return res.customError({ body: 'Execute agent failed!', statusCode: 500, - headers: e.headers, }); } }