-
Notifications
You must be signed in to change notification settings - Fork 176
refac: make admin network errors recoverable when cached data exists #9526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
djbarnwal
wants to merge
1
commit into
main
Choose a base branch
from
refac/network-issue-admin-query
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
web-admin/src/components/errors/admin-network-errors.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import { isAdminServerQuery } from "@rilldata/web-admin/client/utils"; | ||
| import { | ||
| clearAdminNetworkErrorState, | ||
| handleAdminServerNetworkError, | ||
| handleAdminServerQuerySuccess, | ||
| recoverFromAdminNetworkError, | ||
| } from "@rilldata/web-admin/components/errors/admin-network-errors"; | ||
| import { errorStore } from "@rilldata/web-admin/components/errors/error-store"; | ||
| import { eventBus } from "@rilldata/web-common/lib/event-bus/event-bus"; | ||
| import type { Query, QueryClient } from "@tanstack/svelte-query"; | ||
| import { get } from "svelte/store"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| type TestQueryClient = Pick<QueryClient, "refetchQueries">; | ||
|
|
||
| function makeQuery(queryKey: unknown[], data?: unknown): Query { | ||
| return { | ||
| queryKey, | ||
| state: { data }, | ||
| } as unknown as Query; | ||
| } | ||
|
|
||
| function makeQueryClient(): TestQueryClient { | ||
| return { | ||
| refetchQueries: vi.fn(() => Promise.resolve()), | ||
| } as unknown as TestQueryClient; | ||
| } | ||
|
|
||
| describe("admin network errors", () => { | ||
| beforeEach(() => { | ||
| errorStore.reset(); | ||
| vi.spyOn(eventBus, "emit"); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| clearAdminNetworkErrorState(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("keeps cached data visible and shows a banner for admin network errors", () => { | ||
| const queryClient = makeQueryClient(); | ||
| const handled = handleAdminServerNetworkError( | ||
| new Error("Network Error"), | ||
| makeQuery(["/v1/projects/org/proj"], { project: { name: "proj" } }), | ||
| queryClient, | ||
| ); | ||
|
|
||
| expect(handled).toBe(true); | ||
| expect(get(errorStore).header).toBe(""); | ||
| expect(eventBus.emit).toHaveBeenCalledWith( | ||
| "add-banner", | ||
| expect.objectContaining({ | ||
| id: "admin-network", | ||
| message: expect.objectContaining({ | ||
| message: expect.stringContaining("Showing cached data"), | ||
| type: "warning", | ||
| }), | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("uses the full-page error only when there is no cached data", () => { | ||
| const handled = handleAdminServerNetworkError( | ||
| new Error("Network Error"), | ||
| makeQuery(["/v1/projects/org/proj"]), | ||
| makeQueryClient(), | ||
| ); | ||
|
|
||
| expect(handled).toBe(true); | ||
| expect(get(errorStore).header).toBe("Network Error"); | ||
| expect(eventBus.emit).not.toHaveBeenCalledWith( | ||
| "add-banner", | ||
| expect.anything(), | ||
| ); | ||
| }); | ||
|
|
||
| it("ignores non-admin query network errors", () => { | ||
| const handled = handleAdminServerNetworkError( | ||
| new Error("Network Error"), | ||
| makeQuery(["/v1/instances/runtime/api/query"], { rows: [] }), | ||
| makeQueryClient(), | ||
| ); | ||
|
|
||
| expect(handled).toBe(false); | ||
| expect(get(errorStore).header).toBe(""); | ||
| expect(eventBus.emit).not.toHaveBeenCalledWith( | ||
| "add-banner", | ||
| expect.anything(), | ||
| ); | ||
| }); | ||
|
|
||
| it("clears the banner after a successful admin query", () => { | ||
| handleAdminServerNetworkError( | ||
| new Error("Network Error"), | ||
| makeQuery(["/v1/projects/org/proj"], { project: { name: "proj" } }), | ||
| makeQueryClient(), | ||
| ); | ||
| vi.mocked(eventBus.emit).mockClear(); | ||
|
|
||
| handleAdminServerQuerySuccess(makeQuery(["/v1/users/me"], { user: {} })); | ||
|
|
||
| expect(eventBus.emit).toHaveBeenCalledWith( | ||
| "remove-banner", | ||
| "admin-network", | ||
| ); | ||
| }); | ||
|
|
||
| it("refetches active admin queries during recovery", async () => { | ||
| const queryClient = makeQueryClient(); | ||
| handleAdminServerNetworkError( | ||
| new Error("Network Error"), | ||
| makeQuery(["/v1/projects/org/proj"], { project: { name: "proj" } }), | ||
| queryClient, | ||
| ); | ||
|
|
||
| await recoverFromAdminNetworkError(queryClient); | ||
|
|
||
| expect(queryClient.refetchQueries).toHaveBeenCalledWith({ | ||
| type: "active", | ||
| predicate: isAdminServerQuery, | ||
| }); | ||
| }); | ||
|
|
||
| it("treats non-string query keys as non-admin queries", () => { | ||
| expect(isAdminServerQuery(makeQuery([{ path: "/v1/projects" }]))).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
| }); |
110 changes: 110 additions & 0 deletions
110
web-admin/src/components/errors/admin-network-errors.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { isAdminServerQuery } from "@rilldata/web-admin/client/utils"; | ||
| import { errorStore } from "@rilldata/web-admin/components/errors/error-store"; | ||
| import { createUserFacingError } from "@rilldata/web-admin/components/errors/user-facing-errors"; | ||
| import { eventBus } from "@rilldata/web-common/lib/event-bus/event-bus"; | ||
| import type { Query, QueryClient } from "@tanstack/svelte-query"; | ||
|
|
||
| export const AdminNetworkErrorMessage = "Network Error"; | ||
|
|
||
| const AdminNetworkBannerID = "admin-network"; | ||
| const AdminNetworkBannerPriority = 0; | ||
|
|
||
| type QueryRefetcher = Pick<QueryClient, "refetchQueries">; | ||
|
|
||
| let adminNetworkErrorActive = false; | ||
|
|
||
| export function isNetworkError(error: unknown): boolean { | ||
| return error instanceof Error && error.message === AdminNetworkErrorMessage; | ||
| } | ||
|
|
||
| export function handleAdminServerNetworkError( | ||
| error: unknown, | ||
| query: Query, | ||
| queryClient: QueryRefetcher, | ||
| ): boolean { | ||
| if (!isNetworkError(error) || !isAdminServerQuery(query)) return false; | ||
|
|
||
| adminNetworkErrorActive = true; | ||
|
|
||
| if (queryHasCachedData(query)) { | ||
| showAdminNetworkBanner(queryClient); | ||
| } else { | ||
| showAdminNetworkErrorPage(); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| export function handleAdminServerQuerySuccess(query: Query): void { | ||
| if (!adminNetworkErrorActive || !isAdminServerQuery(query)) return; | ||
| clearAdminNetworkErrorState(); | ||
| } | ||
|
|
||
| export function recoverFromAdminNetworkError( | ||
| queryClient: QueryRefetcher, | ||
| ): Promise<unknown> { | ||
| if (!adminNetworkErrorActive) return Promise.resolve(); | ||
|
|
||
| clearAdminNetworkErrorState(); | ||
| return queryClient.refetchQueries({ | ||
| type: "active", | ||
| predicate: isAdminServerQuery, | ||
| }); | ||
| } | ||
|
|
||
| export function registerAdminNetworkRecoveryListeners( | ||
| queryClient: QueryRefetcher, | ||
| ): () => void { | ||
| if (typeof window === "undefined") return () => {}; | ||
|
|
||
| const recover = () => { | ||
| void recoverFromAdminNetworkError(queryClient); | ||
| }; | ||
| const recoverWhenVisible = () => { | ||
| if (document.visibilityState !== "hidden") recover(); | ||
| }; | ||
|
|
||
| window.addEventListener("online", recover); | ||
| window.addEventListener("focus", recoverWhenVisible); | ||
| document.addEventListener("visibilitychange", recoverWhenVisible); | ||
|
|
||
| return () => { | ||
| window.removeEventListener("online", recover); | ||
| window.removeEventListener("focus", recoverWhenVisible); | ||
| document.removeEventListener("visibilitychange", recoverWhenVisible); | ||
| }; | ||
| } | ||
|
|
||
| export function clearAdminNetworkErrorState(): void { | ||
| adminNetworkErrorActive = false; | ||
| errorStore.reset(); | ||
| eventBus.emit("remove-banner", AdminNetworkBannerID); | ||
| } | ||
|
|
||
| function queryHasCachedData(query: Query): boolean { | ||
| return query.state.data !== undefined; | ||
| } | ||
|
|
||
| function showAdminNetworkErrorPage(): void { | ||
| errorStore.set(createUserFacingError(null, AdminNetworkErrorMessage)); | ||
| } | ||
|
|
||
| function showAdminNetworkBanner(queryClient: QueryRefetcher): void { | ||
| eventBus.emit("add-banner", { | ||
| id: AdminNetworkBannerID, | ||
| priority: AdminNetworkBannerPriority, | ||
| message: { | ||
| type: "warning", | ||
| iconType: "alert", | ||
| message: | ||
| "Connection to Rill Cloud was interrupted. Showing cached data while we reconnect.", | ||
| cta: { | ||
| type: "button", | ||
| text: "Retry now", | ||
| async onClick() { | ||
| await recoverFromAdminNetworkError(queryClient); | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this handle all cases? Check web-common/src/lib/errors.ts for some additional types of errors.