From 9dc15872a37a4d6fcb53a5b2a7b6bb4df1715940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Fri, 27 Aug 2021 10:20:03 +0100 Subject: [PATCH] fix(ui): fix typescript 4.4 errors --- ui/src/Hooks/useFetchAny.test.tsx | 22 ++++++++++++++++++++++ ui/src/Hooks/useFetchAny.ts | 5 ++++- ui/src/Hooks/useFetchDelete.test.tsx | 19 +++++++++++++++++++ ui/src/Hooks/useFetchDelete.ts | 4 +++- ui/src/Hooks/useFetchGet.test.tsx | 24 ++++++++++++++++++++++++ ui/src/Hooks/useFetchGet.ts | 3 ++- 6 files changed, 74 insertions(+), 3 deletions(-) diff --git a/ui/src/Hooks/useFetchAny.test.tsx b/ui/src/Hooks/useFetchAny.test.tsx index 21d90355b..cb0174155 100644 --- a/ui/src/Hooks/useFetchAny.test.tsx +++ b/ui/src/Hooks/useFetchAny.test.tsx @@ -22,6 +22,9 @@ describe("useFetchAny", () => { fetchMock.mock("http://localhost/error", { throws: new TypeError("failed to fetch"), }); + fetchMock.mock("http://localhost/unknown", { + throws: "foo", + }); }); beforeEach(() => { @@ -168,6 +171,25 @@ describe("useFetchAny", () => { expect(result.current.responseURI).toBe(null); }); + it("error is updated after unknown error", async () => { + const upstreams = [{ uri: "http://localhost/unknown", options: {} }]; + const { result, waitForNextUpdate } = renderHook(() => + useFetchAny(upstreams) + ); + + expect(result.current.response).toBe(null); + expect(result.current.error).toBe(null); + expect(result.current.inProgress).toBe(true); + expect(result.current.responseURI).toBe(null); + + await waitForNextUpdate(); + + expect(result.current.response).toBe(null); + expect(result.current.error).toBe("unknown error: foo"); + expect(result.current.inProgress).toBe(false); + expect(result.current.responseURI).toBe(null); + }); + it("doesn't update response after cleanup", async () => { fetchMock.mock( "http://localhost/slow/ok", diff --git a/ui/src/Hooks/useFetchAny.ts b/ui/src/Hooks/useFetchAny.ts index 58dfaadc5..8288da1af 100644 --- a/ui/src/Hooks/useFetchAny.ts +++ b/ui/src/Hooks/useFetchAny.ts @@ -107,7 +107,10 @@ const useFetchAny = ( } else { setResponse({ response: null, - error: error.message, + error: + error instanceof Error + ? error.message + : `unknown error: ${error}`, responseURI: null, inProgress: false, }); diff --git a/ui/src/Hooks/useFetchDelete.test.tsx b/ui/src/Hooks/useFetchDelete.test.tsx index ab284d70f..10aacbe17 100644 --- a/ui/src/Hooks/useFetchDelete.test.tsx +++ b/ui/src/Hooks/useFetchDelete.test.tsx @@ -18,6 +18,9 @@ describe("useFetchDelete", () => { fetchMock.mock("http://localhost/error", { throws: new TypeError("failed to fetch"), }); + fetchMock.mock("http://localhost/unknown", { + throws: "foo", + }); }); beforeEach(() => { @@ -108,6 +111,22 @@ describe("useFetchDelete", () => { expect(result.current.isDeleting).toBe(false); }); + it("error is updated after unknown error", async () => { + const { result, waitForNextUpdate } = renderHook(() => + useFetchDelete("http://localhost/unknown", EmptyOptions) + ); + + expect(result.current.response).toBe(null); + expect(result.current.error).toBe(null); + expect(result.current.isDeleting).toBe(true); + + await waitForNextUpdate(); + + expect(result.current.response).toBe(null); + expect(result.current.error).toBe("unknown error: foo"); + expect(result.current.isDeleting).toBe(false); + }); + it("doesn't update response after cleanup", async () => { fetchMock.mock( "http://localhost/slow/ok", diff --git a/ui/src/Hooks/useFetchDelete.ts b/ui/src/Hooks/useFetchDelete.ts index c3a1eb570..57c284cb3 100644 --- a/ui/src/Hooks/useFetchDelete.ts +++ b/ui/src/Hooks/useFetchDelete.ts @@ -45,7 +45,9 @@ const useFetchDelete = ( } } catch (error) { if (!isCancelled) { - setError(error.message); + setError( + error instanceof Error ? error.message : `unknown error: ${error}` + ); setIsDeleting(false); } } diff --git a/ui/src/Hooks/useFetchGet.test.tsx b/ui/src/Hooks/useFetchGet.test.tsx index 07295a83f..82d2fe28e 100644 --- a/ui/src/Hooks/useFetchGet.test.tsx +++ b/ui/src/Hooks/useFetchGet.test.tsx @@ -20,6 +20,9 @@ describe("useFetchGet", () => { fetchMock.mock("http://localhost/error", { throws: new TypeError("failed to fetch"), }); + fetchMock.mock("http://localhost/unknown", { + throws: "foo", + }); }); beforeEach(() => { @@ -207,6 +210,27 @@ describe("useFetchGet", () => { expect(result.current.isRetrying).toBe(false); }); + it("error is updated after unknown error", async () => { + const { result, waitForNextUpdate } = renderHook(() => + useFetchGet("http://localhost/unknown") + ); + + expect(result.current.response).toBe(null); + expect(result.current.error).toBe(null); + expect(result.current.isLoading).toBe(true); + expect(result.current.isRetrying).toBe(false); + + for (let i = 0; i <= FetchRetryConfig.retries; i++) { + jest.runOnlyPendingTimers(); + await waitForNextUpdate(); + } + + expect(result.current.response).toBe(null); + expect(result.current.error).toBe("unknown error: foo"); + expect(result.current.isLoading).toBe(false); + expect(result.current.isRetrying).toBe(false); + }); + it("error is updated on uparsable JSON", async () => { fetchMock.mock("http://localhost/json/invalid", { headers: { "Content-Type": "application/json" }, diff --git a/ui/src/Hooks/useFetchGet.ts b/ui/src/Hooks/useFetchGet.ts index 93a513418..3c3136141 100644 --- a/ui/src/Hooks/useFetchGet.ts +++ b/ui/src/Hooks/useFetchGet.ts @@ -119,7 +119,8 @@ const useFetchGet = ( } catch (error) { setResponse((r) => ({ ...r, - error: error.message, + error: + error instanceof Error ? error.message : `unknown error: ${error}`, isLoading: false, isRetrying: false, }));