From c081817de79b45d1406b2c62ade2a17ee5c970e6 Mon Sep 17 00:00:00 2001 From: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:42:13 +0100 Subject: [PATCH] fix(http_fetcher): show "no connection" only when offline --- js/http_fetcher.mjs | 12 ++++++++++-- tests/unit/functions/http_fetcher_spec.mjs | 20 +++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/js/http_fetcher.mjs b/js/http_fetcher.mjs index bc2538d1e6..7e54eb220f 100644 --- a/js/http_fetcher.mjs +++ b/js/http_fetcher.mjs @@ -21,10 +21,13 @@ const ERROR_TYPE_TO_TRANSLATION = { RATE_LIMITED: "MODULE_ERROR_RATE_LIMITED", SERVER_ERROR: "MODULE_ERROR_SERVER_ERROR", CLIENT_ERROR: "MODULE_ERROR_CLIENT_ERROR", - NETWORK_ERROR: "MODULE_ERROR_NO_CONNECTION", + NETWORK_ERROR: "MODULE_ERROR_UNSPECIFIED", UNKNOWN_ERROR: "MODULE_ERROR_UNSPECIFIED" }; +// Error codes meaning the device is offline, as opposed to a single server being unreachable +const OFFLINE_ERROR_CODES = new Set(["EAI_AGAIN", "ENETDOWN", "ENETUNREACH"]); + /** * HTTPFetcher - Centralized HTTP fetching with intelligent error handling * @@ -357,7 +360,9 @@ class HTTPFetcher extends EventEmitter { } } catch (error) { const isTimeout = error.name === "AbortError"; - const message = isTimeout ? `Request timeout after ${this.timeout}ms` : `Network error: ${error.message}`; + // fetch only says "fetch failed", the actual reason (e.g. ENOTFOUND) is in the cause + const reason = error.cause?.code ?? error.message; + const message = isTimeout ? `Request timeout after ${this.timeout}ms` : `Network error: ${reason}`; this.networkErrorCount = Math.min(this.networkErrorCount + 1, this.maxRetries); const exhausted = this.networkErrorCount >= this.maxRetries; @@ -385,6 +390,9 @@ class HTTPFetcher extends EventEmitter { error, requestUrl ); + if (OFFLINE_ERROR_CODES.has(error.cause?.code)) { + errorInfo.translationKey = "MODULE_ERROR_NO_CONNECTION"; + } this.emit("error", errorInfo); } finally { clearTimeout(timeoutId); diff --git a/tests/unit/functions/http_fetcher_spec.mjs b/tests/unit/functions/http_fetcher_spec.mjs index e86e128357..dc7e4bcc06 100644 --- a/tests/unit/functions/http_fetcher_spec.mjs +++ b/tests/unit/functions/http_fetcher_spec.mjs @@ -89,10 +89,28 @@ describe("HTTPFetcher", () => { const errorInfo = await errorPromise; expect(errorInfo).toHaveProperty("errorType", "NETWORK_ERROR"); - expect(errorInfo).toHaveProperty("translationKey", "MODULE_ERROR_NO_CONNECTION"); expect(errorInfo).toHaveProperty("url", TEST_URL); }); + it.each([ + ["EAI_AGAIN", "MODULE_ERROR_NO_CONNECTION"], + ["ERR_TLS_CERT_ALTNAME_INVALID", "MODULE_ERROR_UNSPECIFIED"] + ])("should report %s as %s and log the reason", async (code, translationKey) => { + const fetchSpy = vi.spyOn(globalThis, "fetch").mockRejectedValue(new TypeError("fetch failed", { cause: Object.assign(new Error(), { code }) })); + fetcher = new HTTPFetcher({ url: TEST_URL, reloadInterval: 60000 }); + + const errorPromise = new Promise((resolve) => { + fetcher.on("error", resolve); + }); + + await fetcher.fetch(); + const errorInfo = await errorPromise; + fetchSpy.mockRestore(); + + expect(errorInfo.translationKey).toBe(translationKey); + expect(errorInfo.message).toBe(`Network error: ${code}`); + }); + it("should emit error event on timeout", async () => { server.use( http.get(TEST_URL, async () => {