Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(frontend): show one error toast when the sample instance fails (#21584)
zchpeter60187c1d57c7
diff --git a/frontend/src/api/context-key/index.ts b/frontend/src/api/context-key/index.tsindex 29aef2076a..790c92b4c3 100644--- a/frontend/src/api/context-key/index.ts+++ b/frontend/src/api/context-key/index.ts@@ -1,2 +1,3 @@export { silentContextKey } from "./silent";export { ignoredCodesContextKey } from "./ignored_codes";+export { skipErrorNotificationContextKey } from "./skip_error_notification";diff --git a/frontend/src/api/context-key/skip_error_notification.ts b/frontend/src/api/context-key/skip_error_notification.tsnew file mode 100644index 0000000000..6bd5680291--- /dev/null+++ b/frontend/src/api/context-key/skip_error_notification.ts@@ -0,0 +1,11 @@+import { createContextKey } from "@connectrpc/connect";++// Unlike silentContextKey, permission-denied redirects and session-expiry+// handling still apply.+export const skipErrorNotificationContextKey = createContextKey<boolean>(+ false,+ {+ description:+ "the caller shows its own error notification, skip the global one",+ }+);diff --git a/frontend/src/api/middlewares/authInterceptorMiddleware.test.ts b/frontend/src/api/middlewares/authInterceptorMiddleware.test.tsindex 33067c414a..9d8e5769de 100644--- a/frontend/src/api/middlewares/authInterceptorMiddleware.test.ts+++ b/frontend/src/api/middlewares/authInterceptorMiddleware.test.ts@@ -1,7 +1,11 @@import { Code, ConnectError, createContextValues } from "@connectrpc/connect";import { beforeEach, describe, expect, test, vi } from "vitest";import type { Permission } from "@/types";-import { ignoredCodesContextKey, silentContextKey } from "../context-key";+import {+ ignoredCodesContextKey,+ silentContextKey,+ skipErrorNotificationContextKey,+} from "../context-key";const mocks = vi.hoisted(() => ({authStore: {@@ -56,15 +60,18 @@ import { authInterceptor } from "./authInterceptorMiddleware";const createRequest = ({methodName = "Chat",silent = false,+ skipErrorNotification = false,ignoredCodes = [],}: {methodName?: string;silent?: boolean;+ skipErrorNotification?: boolean;ignoredCodes?: Code[];} = {}) =>({contextValues: createContextValues().set(silentContextKey, silent)+ .set(skipErrorNotificationContextKey, skipErrorNotification).set(ignoredCodesContextKey, ignoredCodes),method: { name: methodName },service: { name: "TestService", typeName: "bytebase.v1.TestService" },@@ -181,6 +188,33 @@ describe("authInterceptor", () => {});});+ test("keeps redirects and session notices when the caller reports the error", async () => {+ const request = createRequest({ skipErrorNotification: true });++ const denied = vi+ .fn()+ .mockRejectedValue(new ConnectError("forbidden", Code.PermissionDenied));+ await expect(authInterceptor(denied)(request)).rejects.toMatchObject({+ code: Code.PermissionDenied,+ });+ expect(mocks.routerPush).toHaveBeenCalledWith(+ expect.objectContaining({ name: "error.403" })+ );++ const expired = vi+ .fn()+ .mockRejectedValue(+ new ConnectError("access token not found", Code.Unauthenticated)+ );+ mocks.refreshTokens.mockRejectedValue(new Error("refresh failed"));+ await expect(authInterceptor(expired)(request)).rejects.toMatchObject({+ code: Code.Unauthenticated,+ });+ expect(mocks.pushNotification).toHaveBeenCalledWith(+ expect.objectContaining({ title: "auth.token-expired-title" })+ );+ });+test("does not leave sign-in for a rejected web login", async () => {const error = new ConnectError("only users can use web login",diff --git a/frontend/src/api/middlewares/errorNotificationMiddleware.test.ts b/frontend/src/api/middlewares/errorNotificationMiddleware.test.tsindex a6102976a8..8958c70cb1 100644--- a/frontend/src/api/middlewares/errorNotificationMiddleware.test.ts+++ b/frontend/src/api/middlewares/errorNotificationMiddleware.test.ts@@ -1,7 +1,11 @@// @vitest-environment nodeimport { Code, ConnectError, createContextValues } from "@connectrpc/connect";import { beforeEach, describe, expect, test, vi } from "vitest";-import { ignoredCodesContextKey, silentContextKey } from "../context-key";+import {+ ignoredCodesContextKey,+ silentContextKey,+ skipErrorNotificationContextKey,+} from "../context-key";const mocks = vi.hoisted(() => ({pushNotification: vi.fn(),@@ -32,16 +36,19 @@ import { errorNotificationInterceptor } from "./errorNotificationMiddleware";const createRequest = ({silent = false,+ skipErrorNotification = false,ignoredCodes = [],methodName = "ListDatabases",}: {silent?: boolean;+ skipErrorNotification?: boolean;ignoredCodes?: Code[];methodName?: string;} = {}) =>({contextValues: createContextValues().set(silentContextKey, silent)+ .set(skipErrorNotificationContextKey, skipErrorNotification).set(ignoredCodesContextKey, ignoredCodes),method: { name: methodName },service: { name: "DatabaseService" },@@ -103,4 +110,28 @@ describe("errorNotificationInterceptor", () => {description: "[internal] failed",});});++ test("does not toast a silent request failure", async () => {+ const error = new ConnectError("failed", Code.Internal);+ const next = vi.fn().mockRejectedValue(error);++ await expect(+ errorNotificationInterceptor(next)(createRequest({ silent: true }))+ ).rejects.toBe(error);++ expect(mocks.pushNotification).not.toHaveBeenCalled();+ });++ test("leaves the toast to a caller that reports the failure itself", async () => {+ const error = new ConnectError("failed", Code.Internal);+ const next = vi.fn().mockRejectedValue(error);++ await expect(+ errorNotificationInterceptor(next)(+ createRequest({ skipErrorNotification: true })+ )+ ).rejects.toBe(error);++ expect(mocks.pushNotification).not.toHaveBeenCalled();+ });});diff --git a/frontend/src/api/middlewares/errorNotificationMiddleware.ts b/frontend/src/api/middlewares/errorNotificationMiddleware.tsindex 37cbe3eb28..d07acdb19f 100644--- a/frontend/src/api/middlewares/errorNotificationMiddleware.ts+++ b/frontend/src/api/middlewares/errorNotificationMiddleware.ts@@ -1,7 +1,11 @@import { Code, ConnectError, type Interceptor } from "@connectrpc/connect";import i18n from "@/lib/i18n";import { pushNotification } from "@/stores";-import { ignoredCodesContextKey, silentContextKey } from "../context-key";+import {+ ignoredCodesContextKey,+ silentContextKey,+ skipErrorNotificationContextKey,+} from "../context-key";export type SilentRequestOptions = {/**@@ -23,8 +27,12 @@ export const errorNotificationInterceptor: Interceptor =return resp;} catch (error) {const maybePushNotification = (title: string, description?: string) => {- const silent = req.contextValues.get(silentContextKey);- if (silent) return;+ if (+ req.contextValues.get(silentContextKey) ||+ req.contextValues.get(skipErrorNotificationContextKey)+ ) {+ return;+ }pushNotification({module: "bytebase",style: "CRITICAL",diff --git a/frontend/src/components/instance/CreateInstanceView.test.tsx b/frontend/src/components/instance/CreateInstanceView.test.tsxindex f46005a70f..273ac53dd7 100644--- a/frontend/src/components/instance/CreateInstanceView.test.tsx+++ b/frontend/src/components/instance/CreateInstanceView.test.tsx@@ -536,6 +536,7 @@ describe("CreateInstanceView", () => {button.click();});+ expect(mocks.pushNotification).toHaveBeenCalledTimes(1);expect(mocks.pushNotification).toHaveBeenCalledWith({module: "bytebase",style: "CRITICAL",diff --git a/frontend/src/routes/auth/WorkspaceSetupPage.test.tsx b/frontend/src/routes/auth/WorkspaceSetupPage.test.tsxindex aaff5c770d..549650b9a0 100644--- a/frontend/src/routes/auth/WorkspaceSetupPage.test.tsx+++ b/frontend/src/routes/auth/WorkspaceSetupPage.test.tsx@@ -1014,12 +1014,14 @@ describe("WorkspaceSetupPage", () => {await Promise.resolve();});- expect(mocks.pushNotification).toHaveBeenCalledWith(+ const errorNotifications = mocks.pushNotification.mock.calls+ .map(([notification]) => notification)+ .filter((notification) => notification.style === "CRITICAL");+ expect(errorNotifications).toEqual([expect.objectContaining({- style: "CRITICAL",title: "Failed to prepare Sample Project Instance.",- })- );+ }),+ ]);expect(mocks.routerReplace).toHaveBeenCalledWith({name: "workspace.project.detail",params: { projectId: "new-project" },diff --git a/frontend/src/stores/app/instance.test.ts b/frontend/src/stores/app/instance.test.tsindex 99a9a603d5..e77594fa61 100644--- a/frontend/src/stores/app/instance.test.ts+++ b/frontend/src/stores/app/instance.test.ts@@ -1,6 +1,11 @@// @vitest-environment nodeimport { create } from "@bufbuild/protobuf";+import { Code, ConnectError } from "@connectrpc/connect";import { beforeEach, describe, expect, test, vi } from "vitest";+import {+ silentContextKey,+ skipErrorNotificationContextKey,+} from "@/api/context-key";import {InstanceSchema,type ListInstancesRequest,@@ -147,6 +152,33 @@ describe("instance store project parent", () => {});});+ test("leaves a failed sample preparation for the caller to report", async () => {+ const store = createStore();+ const error = new ConnectError(+ "Sample Project Instance is not configured",+ Code.FailedPrecondition+ );+ mocks.prepareSampleProjectInstance.mockRejectedValue(error);++ await expect(+ store.prepareSampleProjectInstance("projects/app")+ ).rejects.toBe(error);++ const options = mocks.prepareSampleProjectInstance.mock.calls[0][1];+ expect(options?.contextValues?.get(skipErrorNotificationContextKey)).toBe(+ true+ );+ });++ test("keeps permission and session handling for a failed sample preparation", async () => {+ const store = createStore();++ await store.prepareSampleProjectInstance("projects/app");++ const options = mocks.prepareSampleProjectInstance.mock.calls[0][1];+ expect(options?.contextValues?.get(silentContextKey)).toBe(false);+ });+test("uses project permission to fetch a nested instance", async () => {const store = createStore();const project = create(ProjectSchema, { name: "projects/app" });diff --git a/frontend/src/stores/app/instance.ts b/frontend/src/stores/app/instance.tsindex 72693f3a96..d63627ede6 100644--- a/frontend/src/stores/app/instance.ts+++ b/frontend/src/stores/app/instance.ts@@ -1,7 +1,10 @@import { create as createProto } from "@bufbuild/protobuf";import { createContextValues } from "@connectrpc/connect";import { instanceServiceClientConnect } from "@/api";-import { silentContextKey } from "@/api/context-key";+import {+ silentContextKey,+ skipErrorNotificationContextKey,+} from "@/api/context-key";import { projectNamePrefix } from "@/stores/modules/v1/common";import { Engine, State } from "@/types/proto-es/v1/common_pb";import {@@ -209,9 +212,17 @@ export const createInstanceSlice: AppSliceCreator<InstanceSlice> = (},prepareSampleProjectInstance: async (parent) => {+ // Every caller reports the failure itself, so the global error toast+ // would show it a second time.const response =await instanceServiceClientConnect.prepareSampleProjectInstance(- createProto(PrepareSampleProjectInstanceRequestSchema, { parent })+ createProto(PrepareSampleProjectInstanceRequestSchema, { parent }),+ {+ contextValues: createContextValues().set(+ skipErrorNotificationContextKey,+ true+ ),+ });const instance = upsertInstances([response])[0];await get()