feat(chat-window): add error handling

This commit is contained in:
ItzCrazyKns 2024-05-04 14:56:54 +05:30
parent 4bf69dfdda
commit e3fef3a1be
No known key found for this signature in database
GPG key ID: 8162927C7CCE3065
4 changed files with 31 additions and 1 deletions

View file

@ -5,6 +5,7 @@ import { Document } from '@langchain/core/documents';
import Navbar from './Navbar';
import Chat from './Chat';
import EmptyChat from './EmptyChat';
import { toast } from 'sonner';
export type Message = {
id: string;
@ -92,17 +93,24 @@ const useSocket = (url: string) => {
wsURL.search = searchParams.toString();
const ws = new WebSocket(wsURL.toString());
ws.onopen = () => {
console.log('[DEBUG] open');
setWs(ws);
};
ws.onmessage = (e) => {
const parsedData = JSON.parse(e.data);
if (parsedData.type === 'error') {
toast.error(parsedData.data);
}
};
};
connectWs();
}
return () => {
1;
ws?.close();
console.log('[DEBUG] closed');
};
@ -150,6 +158,12 @@ const ChatWindow = () => {
const messageHandler = (e: MessageEvent) => {
const data = JSON.parse(e.data);
if (data.type === 'error') {
toast.error(data.data);
setLoading(false);
return;
}
if (data.type === 'sources') {
sources = data.data;
if (!added) {