Skip to content

Commit 693a5a2

Browse files
committed
fix: Update useAutoCompactContinue to handle error objects instead of .catch()
The browser API now returns error objects matching Electron's behavior. Updated the only caller using .catch() to properly check result.success.
1 parent 7260cc8 commit 693a5a2

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

src/browser/api.test.ts

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,10 @@ function createInvokeIPC(
4242

4343
const result = (await response.json()) as InvokeResponse<T>;
4444

45+
// Return the result as-is - let the caller handle success/failure
46+
// This matches the behavior of Electron's ipcRenderer.invoke() which doesn't throw on error
4547
if (!result.success) {
46-
// Failed response - check if it's a structured error or simple string
47-
if (typeof result.error === "object" && result.error !== null) {
48-
// Structured error (e.g., SendMessageError) - return as Result<T, E> for caller to handle
49-
return result as T;
50-
}
51-
// Simple string error - throw it
52-
throw new Error(typeof result.error === "string" ? result.error : "Unknown error");
48+
return result as T;
5349
}
5450

5551
// Success - unwrap and return the data
@@ -60,31 +56,16 @@ function createInvokeIPC(
6056
}
6157

6258
describe("Browser API invokeIPC", () => {
63-
test("CURRENT BEHAVIOR: throws on string error (causes unhandled rejection)", async () => {
59+
test("should return error object on failure (matches Electron behavior)", async () => {
6460
const mockFetch = createMockFetch({
6561
success: false,
6662
error: "fatal: contains modified or untracked files",
6763
});
6864

6965
const invokeIPC = createInvokeIPC(mockFetch);
7066

71-
// Current behavior: invokeIPC throws on string errors
72-
// eslint-disable-next-line @typescript-eslint/await-thenable
73-
await expect(invokeIPC("WORKSPACE_REMOVE", "test-workspace", { force: false })).rejects.toThrow(
74-
"fatal: contains modified or untracked files"
75-
);
76-
});
77-
78-
test.skip("DESIRED BEHAVIOR: should return error object on string error (match Electron)", async () => {
79-
const mockFetch = createMockFetch({
80-
success: false,
81-
error: "fatal: contains modified or untracked files",
82-
});
83-
84-
const invokeIPC = createInvokeIPC(mockFetch);
85-
86-
// Desired behavior: Should return { success: false, error: "..." }
87-
// This test documents what we want - actual implementation test is below
67+
// Fixed behavior: invokeIPC returns error object instead of throwing
68+
// This matches Electron's ipcRenderer.invoke() which never throws on error
8869
const result = await invokeIPC<{ success: boolean; error?: string }>(
8970
"WORKSPACE_REMOVE",
9071
"test-workspace",

src/hooks/useAutoCompactContinue.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,25 @@ export function useAutoCompactContinue() {
8181

8282
// Build options and send message directly
8383
const options = buildSendMessageOptions(workspaceId);
84-
window.api.workspace.sendMessage(workspaceId, continueMessage, options).catch((error) => {
85-
console.error("Failed to send continue message:", error);
86-
// If sending failed, remove from processed set to allow retry
87-
processedMessageIds.current.delete(idForGuard);
88-
});
84+
void (async () => {
85+
try {
86+
const result = await window.api.workspace.sendMessage(
87+
workspaceId,
88+
continueMessage,
89+
options
90+
);
91+
// Check if send failed (browser API returns error object, not throw)
92+
if (!result.success && "error" in result) {
93+
console.error("Failed to send continue message:", result.error);
94+
// If sending failed, remove from processed set to allow retry
95+
processedMessageIds.current.delete(idForGuard);
96+
}
97+
} catch (error) {
98+
// Handle network/parsing errors (HTTP errors, etc.)
99+
console.error("Failed to send continue message:", error);
100+
processedMessageIds.current.delete(idForGuard);
101+
}
102+
})();
89103
}
90104
};
91105

0 commit comments

Comments
 (0)