From c65c170088af8824aec1af510cc08d81d522d032 Mon Sep 17 00:00:00 2001 From: Mohammad Kermani Date: Mon, 7 Apr 2025 08:59:00 +0000 Subject: [PATCH 1/2] fix: show actual error message when chat fails --- components/chat.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/chat.tsx b/components/chat.tsx index 6b1367d..fd89296 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -48,7 +48,7 @@ export function Chat({ mutate('/api/history'); }, onError: (error) => { - toast.error('An error occured, please try again!'); + toast.error(error.message); }, }); From c67299f7b9617dab1d5318a976b803c96ee3fa82 Mon Sep 17 00:00:00 2001 From: Mohammad Kermani Date: Mon, 7 Apr 2025 11:24:14 +0000 Subject: [PATCH 2/2] chore: improve error messages shown to user --- app/(auth)/adapter.ts | 28 ++++++++++++++--- app/(chat)/adapter.ts | 59 +++++++++++++++++++++++++++++++----- app/(chat)/api/chat/route.ts | 8 ++++- lib/ai/pattern-model.ts | 7 +---- 4 files changed, 84 insertions(+), 18 deletions(-) diff --git a/app/(auth)/adapter.ts b/app/(auth)/adapter.ts index 85e9d40..9e176a3 100644 --- a/app/(auth)/adapter.ts +++ b/app/(auth)/adapter.ts @@ -44,7 +44,12 @@ export const getAllWorkspaces = async ( `Fetching workspaces failed with error code ${allWorkspacesResponse.status}`, ); } catch (error) { - return Err(extractErrorMessageOrDefault(error)); + return Err( + extractErrorMessageOrDefault( + error, + 'Unknown error while fetching workspaces', + ), + ); } }; @@ -76,7 +81,12 @@ export const createWorkspace = async ( } return Err(`Creating workspace failed with error code ${response.status}`); } catch (error) { - return Err(extractErrorMessageOrDefault(error)); + return Err( + extractErrorMessageOrDefault( + error, + 'Unknown error while creating workspace', + ), + ); } }; @@ -106,7 +116,12 @@ export const getAllProjects = async ( `Fetching projects failed with error code ${allProjectsResponse.status}`, ); } catch (error) { - return Err(extractErrorMessageOrDefault(error)); + return Err( + extractErrorMessageOrDefault( + error, + 'Unknown error while fetching projects', + ), + ); } }; @@ -140,6 +155,11 @@ export const createProjectInWorkspace = async ( } return Err(`Creating project failed with error code ${response.status}`); } catch (error) { - return Err(extractErrorMessageOrDefault(error)); + return Err( + extractErrorMessageOrDefault( + error, + 'Unknown error while creating project', + ), + ); } }; diff --git a/app/(chat)/adapter.ts b/app/(chat)/adapter.ts index f03a530..38e04ae 100644 --- a/app/(chat)/adapter.ts +++ b/app/(chat)/adapter.ts @@ -53,7 +53,12 @@ export const getConversation = async ( `Fetching conversation failed with error code ${conversationResponse.status}`, ); } catch (error) { - return Err(extractErrorMessageOrDefault(error)); + return Err( + extractErrorMessageOrDefault( + error, + 'Unknown error while fetching conversation', + ), + ); } }; @@ -91,7 +96,12 @@ export const getConversationMessages = async ( `Fetching conversation messages failed with error code ${conversationResponse.status}`, ); } catch (error) { - return Err(extractErrorMessageOrDefault(error)); + return Err( + extractErrorMessageOrDefault( + error, + 'Unknown error while fetching conversation messages', + ), + ); } }; @@ -138,7 +148,12 @@ export const createConversation = async ( `Creating conversation failed with error code ${conversationResponse.status}`, ); } catch (error) { - return Err(extractErrorMessageOrDefault(error)); + return Err( + extractErrorMessageOrDefault( + error, + 'Unknown error while creating conversation', + ), + ); } }; @@ -185,7 +200,12 @@ export const sendMessage = async ( `Sending message failed with error code ${messageResponse.status}`, ); } catch (error) { - return Err(extractErrorMessageOrDefault(error)); + return Err( + extractErrorMessageOrDefault( + error, + 'Unknown error while sending message', + ), + ); } }; @@ -228,11 +248,26 @@ export const sendMessageStreamed = async ( : Err('Message was sent but stream object is null'); } + /** + * This error is specifically handled because the user should know the + * reason, so that they can top up their MOR stake if needed + */ + if (messageResponse.status === 429) { + return Err( + 'You have used all your daily prompt credits. Please top up your MOR stake to continue, or wait until tomorrow.', + ); + } + return Err( `Sending message failed with error code ${messageResponse.status}`, ); } catch (error) { - return Err(extractErrorMessageOrDefault(error)); + return Err( + extractErrorMessageOrDefault( + error, + 'Unknown error while sending streamed message', + ), + ); } }; @@ -267,7 +302,12 @@ export const getAllConversations = async ( `Fetching projects failed with error code ${allConversationsResponse.status}`, ); } catch (error) { - return Err(extractErrorMessageOrDefault(error)); + return Err( + extractErrorMessageOrDefault( + error, + 'Unknown error while fetching all conversations', + ), + ); } }; @@ -311,6 +351,11 @@ export const renameConversation = async ( `Renaming conversation failed with error code ${renameResponse.status}`, ); } catch (error) { - return Err(extractErrorMessageOrDefault(error)); + return Err( + extractErrorMessageOrDefault( + error, + 'Unknown error while renaming conversation', + ), + ); } }; diff --git a/app/(chat)/api/chat/route.ts b/app/(chat)/api/chat/route.ts index c129956..7935560 100644 --- a/app/(chat)/api/chat/route.ts +++ b/app/(chat)/api/chat/route.ts @@ -75,7 +75,13 @@ export async function POST(request: Request) { }); }, onError: (error) => { - return extractErrorMessageOrDefault(error); + /** + * We may get an error object when calling message API, or an error string + * when transforming the stream + */ + return typeof error === 'string' + ? error + : extractErrorMessageOrDefault(error); }, }); } diff --git a/lib/ai/pattern-model.ts b/lib/ai/pattern-model.ts index 3770278..24b1b5b 100644 --- a/lib/ai/pattern-model.ts +++ b/lib/ai/pattern-model.ts @@ -11,8 +11,6 @@ import type { ToolStartEvent, } from '@/lib/ai/types'; -import { extractErrorMessageOrDefault } from '../utils'; - const textDecoder = new TextDecoder(); export class PatternModel implements LanguageModelV1 { @@ -115,10 +113,7 @@ export class PatternModel implements LanguageModelV1 { } catch (error) { controller.enqueue({ type: 'error', - error: extractErrorMessageOrDefault( - error, - 'An unknown error occurred when transforming response chunk', - ), + error: 'Cannot parse chunk due to corrupted data or invalid JSON', }); } } else {