mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
fix: preserve prompt on network error, swallow 524 to avoid duplicate sends
This commit is contained in:
1 parent
909effee28
commit
da28f58706
4 files changed
+51
-2
No files matched your search
@@ -9,7 +9,7 @@ import type {
|
||||
MessageWithParts,
|
||||
} from "../api/types";
|
||||
import type { paths, components } from "../api/opencode-types";
|
||||
import { parseNetworkError } from "../lib/opencode-errors";
|
||||
import { parseNetworkError, isGatewayTimeout } from "../lib/opencode-errors";
|
||||
import { showToast } from "../lib/toast";
|
||||
import { useSendErrorStore } from "../stores/sendErrorStore";
|
||||
import { useSessionStatus } from "../stores/sessionStatusStore";
|
||||
@@ -510,6 +510,10 @@ export const useSendPrompt = (opcodeUrl: string | null | undefined, directory?:
|
||||
(old) => old?.filter((msgWithParts) => !msgWithParts.info.id.startsWith("optimistic_")),
|
||||
);
|
||||
|
||||
if (isGatewayTimeout(error)) {
|
||||
return;
|
||||
}
|
||||
|
||||
useSessionStatus.getState().clearStatus(sessionID);
|
||||
|
||||
const parsed = parseNetworkError(error);
|
||||
|
||||
@@ -41,6 +41,7 @@ vi.mock('../lib/opencode-errors', () => ({
|
||||
message: err.message,
|
||||
isRetryable: false,
|
||||
})),
|
||||
isGatewayTimeout: vi.fn((err) => err?.statusCode === 524),
|
||||
}))
|
||||
|
||||
const mockClearError = vi.fn()
|
||||
@@ -308,4 +309,25 @@ describe('useSendPrompt', () => {
|
||||
failedPrompt: 'queued while disconnected',
|
||||
}))
|
||||
})
|
||||
|
||||
it('rolls back optimistic message but keeps status and surfaces no error on gateway timeout', async () => {
|
||||
const queryKey = messagesQueryKey('http://localhost:5551', 'session-524', '/test')
|
||||
queryClient.setQueryData(queryKey, [
|
||||
{ info: { id: 'optimistic_user_1' }, parts: [] },
|
||||
])
|
||||
mockSendPrompt.mockRejectedValueOnce(new FetchError('Gateway timeout', 524))
|
||||
|
||||
const { result } = renderHookWithProviders()
|
||||
|
||||
await expect(
|
||||
result.current.mutateAsync({
|
||||
sessionID: 'session-524',
|
||||
prompt: 'long running prompt',
|
||||
})
|
||||
).rejects.toThrow('Gateway timeout')
|
||||
|
||||
expect(queryClient.getQueryData(queryKey)).toEqual([])
|
||||
expect(mockClearStatus).not.toHaveBeenCalled()
|
||||
expect(mockSetError).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { FetchError } from '@opencode-manager/shared'
|
||||
import { parseNetworkError } from './opencode-errors'
|
||||
import { parseNetworkError, isGatewayTimeout } from './opencode-errors'
|
||||
|
||||
describe('parseNetworkError', () => {
|
||||
it('classifies TypeError("Failed to fetch") as Connection Failed', () => {
|
||||
@@ -93,3 +93,22 @@ describe('parseNetworkError', () => {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('isGatewayTimeout', () => {
|
||||
it('returns true for a FetchError with statusCode 524', () => {
|
||||
expect(isGatewayTimeout(new FetchError('Gateway timeout', 524))).toBe(true)
|
||||
})
|
||||
|
||||
it('returns false for a FetchError with statusCode 502', () => {
|
||||
expect(isGatewayTimeout(new FetchError('Bad gateway', 502))).toBe(false)
|
||||
})
|
||||
|
||||
it('returns false for a TypeError', () => {
|
||||
expect(isGatewayTimeout(new TypeError('Failed to fetch'))).toBe(false)
|
||||
})
|
||||
|
||||
it('returns false for non-error values', () => {
|
||||
expect(isGatewayTimeout(undefined)).toBe(false)
|
||||
expect(isGatewayTimeout(524)).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -62,6 +62,10 @@ export function parseOpenCodeError(error: OpenCodeError | undefined | null): Par
|
||||
}
|
||||
}
|
||||
|
||||
export function isGatewayTimeout(error: unknown): boolean {
|
||||
return error instanceof FetchError && error.statusCode === 524
|
||||
}
|
||||
|
||||
export function parseNetworkError(error: unknown): ParsedError {
|
||||
if (error instanceof Error) {
|
||||
if (error instanceof FetchError) {
|
||||
|
||||
Reference in new issue
Block a user