Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): no error reporting if fetch fails

Elian Doranf95b68cdf377
diff --git a/apps/client/src/services/llm_chat.ts b/apps/client/src/services/llm_chat.ts
index cd6ab3e63f..aecabba7aa 100644
--- a/apps/client/src/services/llm_chat.ts
+++ b/apps/client/src/services/llm_chat.ts
@@ -30,17 +30,29 @@ export async function streamChatCompletion(
callbacks: StreamCallbacks,
abortSignal?: AbortSignal
): Promise<void> {
- const headers = await server.getHeaders();
-
- const response = await fetch(`${window.glob.baseApiUrl}llm-chat/stream`, {
- method: "POST",
- headers: {
- ...headers,
- "Content-Type": "application/json"
- } as HeadersInit,
- body: JSON.stringify({ messages, config }),
- signal: abortSignal
- });
+ let response: Response;
+ try {
+ const headers = await server.getHeaders();
+ response = await fetch(`${window.glob.baseApiUrl}llm-chat/stream`, {
+ method: "POST",
+ headers: {
+ ...headers,
+ "Content-Type": "application/json"
+ } as HeadersInit,
+ body: JSON.stringify({ messages, config }),
+ signal: abortSignal
+ });
+ } catch (e) {
+ // AbortError is the user stopping generation — let the caller handle it.
+ // Everything else (network failure, custom-protocol/CORS issues, DNS, etc.)
+ // is reported via onError so the chat UI shows it instead of hanging.
+ if (e instanceof DOMException && e.name === "AbortError") {
+ throw e;
+ }
+ const message = e instanceof Error ? e.message : String(e);
+ callbacks.onError(`Failed to connect to LLM stream: ${message}`);
+ return;
+ }
if (!response.ok) {
callbacks.onError(`HTTP ${response.status}: ${response.statusText}`);
@@ -110,6 +122,12 @@ export async function streamChatCompletion(
}
}
}
+ } catch (e) {
+ if (e instanceof DOMException && e.name === "AbortError") {
+ throw e;
+ }
+ const message = e instanceof Error ? e.message : String(e);
+ callbacks.onError(`LLM stream interrupted: ${message}`);
} finally {
reader.releaseLock();
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
index 30579c5a59..d04ff81a02 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
@@ -396,10 +396,25 @@ export function useLlmChat(
// AbortError is expected when user stops generation
if (e instanceof DOMException && e.name === "AbortError") {
finalizeStream();
- } else {
- // Re-throw other errors so they are not swallowed
- throw e;
+ return;
}
+ // Unexpected error — streamChatCompletion normally routes failures
+ // through onError, so reaching here is a bug. Surface it in the chat
+ // and reset state instead of leaving the UI stuck on the stop button.
+ console.error("Unexpected error in chat stream:", e);
+ const errorMsg = e instanceof Error ? e.message : String(e);
+ setMessages([...conversation, {
+ id: randomString(),
+ role: "assistant",
+ content: errorMsg,
+ createdAt: new Date().toISOString(),
+ type: "error"
+ }]);
+ setStreamingContent("");
+ setStreamingBlocks([]);
+ setStreamingThinking("");
+ setIsStreaming(false);
+ abortControllerRef.current = null;
});
}, [selectedModel, availableModels, enableWebSearch, enableNoteTools, enableExtendedThinking, contextNoteId, supportsExtendedThinking, setMessages]);