feat(ws-managers): implement better error handling

This commit is contained in:
ItzCrazyKns 2024-05-06 19:59:13 +05:30
parent ed47191d9b
commit e8fe74ae7c
No known key found for this signature in database
GPG Key ID: 8162927C7CCE3065
2 changed files with 71 additions and 58 deletions

View File

@ -14,6 +14,7 @@ export const handleConnection = async (
ws: WebSocket,
request: IncomingMessage,
) => {
try {
const searchParams = new URL(request.url, `http://${request.headers.host}`)
.searchParams;
@ -23,7 +24,8 @@ export const handleConnection = async (
]);
const chatModelProvider =
searchParams.get('chatModelProvider') || Object.keys(chatModelProviders)[0];
searchParams.get('chatModelProvider') ||
Object.keys(chatModelProviders)[0];
const chatModel =
searchParams.get('chatModel') ||
Object.keys(chatModelProviders[chatModelProvider])[0];
@ -84,4 +86,15 @@ export const handleConnection = async (
);
ws.on('close', () => logger.debug('Connection closed'));
} catch (err) {
ws.send(
JSON.stringify({
type: 'error',
data: 'Internal server error.',
key: 'INTERNAL_SERVER_ERROR',
}),
);
ws.close();
logger.error(err);
}
};

View File

@ -50,13 +50,13 @@ const useSocket = (url: string) => {
!chatModelProviders ||
Object.keys(chatModelProviders).length === 0
)
return console.error('No chat models available');
return toast.error('No chat models available');
if (
!embeddingModelProviders ||
Object.keys(embeddingModelProviders).length === 0
)
return console.error('No embedding models available');
return toast.error('No embedding models available');
chatModelProvider = Object.keys(chatModelProviders)[0];
chatModel = Object.keys(chatModelProviders[chatModelProvider])[0];