From 03aa9d0c82b134aee059b1b44b466bfdc8e85c06 Mon Sep 17 00:00:00 2001 From: Matias Espinoza Date: Mon, 13 Mar 2023 12:52:58 -0300 Subject: [PATCH] Improved support for error handling --- src/api/index.ts | 8 ++++++-- src/bot/commands/chatCommand.ts | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index 2afb00b..751b2a3 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -31,8 +31,12 @@ export class Api implements AI, Runnable { const request = await this._api.createChatCompletion({ model: 'gpt-3.5-turbo', messages: chatHistory, - }); + }).then((response) => response.data.choices[0].message) + .catch((error: Error) => { + this._logger.service.error(error.message); + throw error; + }); - return (request.data.choices[0].message as ChatCompletionResponseMessage); + return (request as ChatCompletionResponseMessage); } } diff --git a/src/bot/commands/chatCommand.ts b/src/bot/commands/chatCommand.ts index 10ed0f8..e82b08d 100644 --- a/src/bot/commands/chatCommand.ts +++ b/src/bot/commands/chatCommand.ts @@ -40,7 +40,7 @@ export const ChatCommand: Command = { embed.forEach((item) => { const message: ChatCompletionRequestMessage = { role: item.footer?.text === 'embed-question' ? 'user' : 'assistant', - content: item.description || 'Please notify an error in process', + content: item.description || 'An error occurred during the process, please try again later.', }; chatHistory.push(message); @@ -52,14 +52,14 @@ export const ChatCommand: Command = { const currentQuestion: ChatCompletionRequestMessage = { role: 'user', - content: question || 'Please notify an error in process', + content: question || 'An error occurred during the process, please try again later.', }; chatHistory.push(currentQuestion); const answer = await ai?.chatCompletion(chatHistory) .then((response) => response.content) - .catch((error) => error); + .catch((error: Error) => error.message); await interaction.followUp({ ephemeral,