From 45abad5333738808d8d5eaef0c2cb1a6224f9145 Mon Sep 17 00:00:00 2001 From: yuzcat01 Date: Sat, 9 Nov 2024 14:41:16 +0800 Subject: [PATCH 1/2] Add a error prompt and a timeout timer --- src/extension/api.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/extension/api.ts b/src/extension/api.ts index 108dcf08..998ff00a 100644 --- a/src/extension/api.ts +++ b/src/extension/api.ts @@ -8,6 +8,11 @@ export async function streamResponse(request: StreamRequest) { const controller = new AbortController() const { signal } = controller + const timeOut = setTimeout(() => { + controller.abort() + onError?.(new Error("Request timed out")) + }, 10000) + try { const url = `${options.protocol}://${options.hostname}${options.port ? `:${options.port}` : ""}${options.path}` const fetchOptions = { @@ -18,6 +23,7 @@ export async function streamResponse(request: StreamRequest) { } const response = await fetch(url, fetchOptions) + clearTimeout(timeOut) if (!response.ok) { throw new Error(`Server responded with status code: ${response.status}`) @@ -77,12 +83,14 @@ export async function streamResponse(request: StreamRequest) { onEnd?.() reader.releaseLock() } catch (error: unknown) { + clearTimeout(timeOut) controller.abort() if (error instanceof Error) { if (error.name === "AbortError") { onEnd?.() } else { console.error("Fetch error:", error) + onError?.(error) } } } From 9b7ea139c75a53692178f286c6767ed204329ae6 Mon Sep 17 00:00:00 2001 From: yuzcat01 <127290792+yuzcat01@users.noreply.github.com> Date: Mon, 11 Nov 2024 14:11:22 +0800 Subject: [PATCH 2/2] lengthen the waiting time of timeOut --- src/extension/api.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extension/api.ts b/src/extension/api.ts index 998ff00a..db3c41c0 100644 --- a/src/extension/api.ts +++ b/src/extension/api.ts @@ -11,7 +11,7 @@ export async function streamResponse(request: StreamRequest) { const timeOut = setTimeout(() => { controller.abort() onError?.(new Error("Request timed out")) - }, 10000) + }, 25000) try { const url = `${options.protocol}://${options.hostname}${options.port ? `:${options.port}` : ""}${options.path}`