Skip to content

Commit 8d982c4

Browse files
committed
fix(chat): catch rethrown save errors in fire-and-forget callers
handleSaveAsNote intentionally rethrows so that the programmatic autosaveCurrentChat path can detect failures and abort project switches. However, two UI-driven callers (handleNewChat and the manual Save button) did not catch the rethrown error, producing unhandled rejections. Wrap both with .catch(() => {}) since the error is already logged and noticed inside handleSaveAsNote itself.
1 parent 7766943 commit 8d982c4

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

src/components/Chat.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,9 +704,11 @@ const ChatInternal: React.FC<ChatProps & { chatInput: ReturnType<typeof useChatI
704704
}
705705
}
706706

707-
// First autosave the current chat if the setting is enabled
707+
// First autosave the current chat if the setting is enabled.
708+
// Reason: catch here because handleNewChat is called from an onClick handler
709+
// where a rejected promise would become an unhandled rejection.
708710
if (settings.autosaveChat) {
709-
await handleSaveAsNote();
711+
await handleSaveAsNote().catch(() => {});
710712
}
711713

712714
// Clear messages through the new architecture
@@ -896,7 +898,7 @@ const ChatInternal: React.FC<ChatProps & { chatInput: ReturnType<typeof useChatI
896898
<>
897899
<ChatControls
898900
onNewChat={handleNewChat}
899-
onSaveAsNote={() => handleSaveAsNote()}
901+
onSaveAsNote={() => handleSaveAsNote().catch(() => {})}
900902
onLoadHistory={handleLoadChatHistory}
901903
onModeChange={(newMode) => {
902904
setPreviousMode(selectedChain);

src/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,14 @@ export default class CopilotPlugin extends Plugin {
289289
if (getSettings().autosaveChat) {
290290
const chatView = this.app.workspace.getLeavesOfType(CHAT_VIEWTYPE)[0]?.view as CopilotView;
291291
if (chatView) {
292-
await chatView.saveChat(projectOverride);
292+
if (projectOverride !== undefined) {
293+
// Reason: project-switch path needs to detect save failure to abort the switch.
294+
await chatView.saveChat(projectOverride);
295+
} else {
296+
// Reason: non-switch callers (loadChatHistory, newChat, settings reload) should
297+
// not be blocked by a transient save error. Error is already logged+noticed inside.
298+
await chatView.saveChat().catch(() => {});
299+
}
293300
}
294301
}
295302
}

0 commit comments

Comments
 (0)