From 76c4b9193a5ac818eee889487710546e858c2d2a Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 28 Jun 2024 13:37:58 +0700 Subject: [PATCH] chore: error handling for chat/completions endpoint --- .../controllers/chat.controller.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/cortex-js/src/infrastructure/controllers/chat.controller.ts b/cortex-js/src/infrastructure/controllers/chat.controller.ts index 25524bc10..f86e6bbf4 100644 --- a/cortex-js/src/infrastructure/controllers/chat.controller.ts +++ b/cortex-js/src/infrastructure/controllers/chat.controller.ts @@ -29,13 +29,25 @@ export class ChatController { const { stream } = createChatDto; if (stream) { - res.header('Content-Type', 'text/event-stream'); this.chatService .inference(createChatDto, headers) - .then((stream) => stream.pipe(res)); + .then((stream) => { + res.header('Content-Type', 'text/event-stream'); + stream.pipe(res); + }) + .catch((error) => + res.status(error.statusCode ?? 400).send(error.message), + ); } else { res.header('Content-Type', 'application/json'); - res.json(await this.chatService.inference(createChatDto, headers)); + this.chatService + .inference(createChatDto, headers) + .then((response) => { + res.json(response); + }) + .catch((error) => + res.status(error.statusCode ?? 400).send(error.message), + ); } } }