From 3bb94da804a373c55e1660e3b746b9a8da44958c Mon Sep 17 00:00:00 2001
From: Thuc Pham <51660321+thucpn@users.noreply.github.com>
Date: Mon, 22 Apr 2024 09:06:19 +0700
Subject: [PATCH] feat: show alert when getting chat error (#55)

---
 templates/components/engines/python/chat/__init__.py      | 8 ++++++--
 .../express/src/controllers/chat-request.controller.ts    | 2 +-
 .../streaming/express/src/controllers/chat.controller.ts  | 2 +-
 templates/types/streaming/nextjs/app/api/chat/route.ts    | 2 +-
 .../streaming/nextjs/app/components/chat-section.tsx      | 4 ++++
 5 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/templates/components/engines/python/chat/__init__.py b/templates/components/engines/python/chat/__init__.py
index cec97256..80bcc41a 100644
--- a/templates/components/engines/python/chat/__init__.py
+++ b/templates/components/engines/python/chat/__init__.py
@@ -1,5 +1,6 @@
 import os
 from app.engine.index import get_index
+from fastapi import HTTPException
 
 
 def get_chat_engine():
@@ -8,8 +9,11 @@ def get_chat_engine():
 
     index = get_index()
     if index is None:
-        raise Exception(
-            "StorageContext is empty - call 'poetry run generate' to generate the storage first"
+        raise HTTPException(
+            status_code=500,
+            detail=str(
+                "StorageContext is empty - call 'poetry run generate' to generate the storage first"
+            ),
         )
 
     return index.as_chat_engine(
diff --git a/templates/types/streaming/express/src/controllers/chat-request.controller.ts b/templates/types/streaming/express/src/controllers/chat-request.controller.ts
index 056edc5d..117713fb 100644
--- a/templates/types/streaming/express/src/controllers/chat-request.controller.ts
+++ b/templates/types/streaming/express/src/controllers/chat-request.controller.ts
@@ -57,7 +57,7 @@ export const chatRequest = async (req: Request, res: Response) => {
   } catch (error) {
     console.error("[LlamaIndex]", error);
     return res.status(500).json({
-      error: (error as Error).message,
+      detail: (error as Error).message,
     });
   }
 };
diff --git a/templates/types/streaming/express/src/controllers/chat.controller.ts b/templates/types/streaming/express/src/controllers/chat.controller.ts
index 7bdf98da..f200628a 100644
--- a/templates/types/streaming/express/src/controllers/chat.controller.ts
+++ b/templates/types/streaming/express/src/controllers/chat.controller.ts
@@ -71,7 +71,7 @@ export const chat = async (req: Request, res: Response) => {
   } catch (error) {
     console.error("[LlamaIndex]", error);
     return res.status(500).json({
-      error: (error as Error).message,
+      detail: (error as Error).message,
     });
   }
 };
diff --git a/templates/types/streaming/nextjs/app/api/chat/route.ts b/templates/types/streaming/nextjs/app/api/chat/route.ts
index 92e874bb..f836cbed 100644
--- a/templates/types/streaming/nextjs/app/api/chat/route.ts
+++ b/templates/types/streaming/nextjs/app/api/chat/route.ts
@@ -74,7 +74,7 @@ export async function POST(request: NextRequest) {
     console.error("[LlamaIndex]", error);
     return NextResponse.json(
       {
-        error: (error as Error).message,
+        detail: (error as Error).message,
       },
       {
         status: 500,
diff --git a/templates/types/streaming/nextjs/app/components/chat-section.tsx b/templates/types/streaming/nextjs/app/components/chat-section.tsx
index ceb31c9e..e9860746 100644
--- a/templates/types/streaming/nextjs/app/components/chat-section.tsx
+++ b/templates/types/streaming/nextjs/app/components/chat-section.tsx
@@ -17,6 +17,10 @@ export default function ChatSection() {
     headers: {
       "Content-Type": "application/json", // using JSON because of vercel/ai 2.2.26
     },
+    onError: (error) => {
+      const message = JSON.parse(error.message);
+      alert(message.detail);
+    },
   });
 
   return (
-- 
GitLab