From 4bcca2a4798e213120ea83fc6ea953d5571b19b7 Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Wed, 22 Nov 2023 13:32:05 +0800 Subject: [PATCH] feat: add error handling Signed-off-by: SuZhou-Joe --- server/utils/message_parser_runner.test.ts | 28 ++++++++++++++++++++++ server/utils/message_parser_runner.ts | 6 +++++ 2 files changed, 34 insertions(+) diff --git a/server/utils/message_parser_runner.test.ts b/server/utils/message_parser_runner.test.ts index 97238ecf..e931f47a 100644 --- a/server/utils/message_parser_runner.test.ts +++ b/server/utils/message_parser_runner.test.ts @@ -119,4 +119,32 @@ describe('MessageParserRunner', () => { }, ]); }); + + it('Do not append messages that are throwed with error or not an array', async () => { + const messageParserRunner = new MessageParserRunner([ + { + id: 'test_with_error', + parserProvider() { + throw new Error('error'); + }, + }, + { + id: 'test_with_incorrect_format_of_return', + parserProvider() { + return Promise.resolve({ + type: 'output', + contentType: 'markdown', + content: 'order1000', + }); + }, + }, + ]); + + expect( + await messageParserRunner.run({ + response: 'output', + input: 'input', + }) + ).toEqual([]); + }); }); diff --git a/server/utils/message_parser_runner.ts b/server/utils/message_parser_runner.ts index 97972d17..2f5d7d59 100644 --- a/server/utils/message_parser_runner.ts +++ b/server/utils/message_parser_runner.ts @@ -20,6 +20,12 @@ export class MessageParserRunner { let tempResult: IMessage[] = []; try { tempResult = await messageParser.parserProvider(interaction); + /** + * Make sure the tempResult is an array. + */ + if (!Array.isArray(tempResult)) { + tempResult = []; + } } catch (e) { tempResult = []; }