Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore(frontend): introduce grpc-web error notification middleware (#6544)
Liu Ji9452df23aa04
diff --git a/frontend/src/grpcweb/index.ts b/frontend/src/grpcweb/index.tsindex 8e8bb8c57f..7d26aa157c 100644--- a/frontend/src/grpcweb/index.ts+++ b/frontend/src/grpcweb/index.ts@@ -3,7 +3,10 @@ import {createClientFactory,FetchTransport,} from "nice-grpc-web";-import { authInterceptorMiddleware } from "./middlewares";+import {+ authInterceptorMiddleware,+ errorNotificationMiddleware,+} from "./middlewares";import { AuthServiceDefinition } from "@/types/proto/v1/auth_service";import { RoleServiceDefinition } from "@/types/proto/v1/role_service";import { IdentityProviderServiceDefinition } from "@/types/proto/v1/idp_service";@@ -37,7 +40,18 @@ const channel = createChannel(}));-const clientFactory = createClientFactory().use(authInterceptorMiddleware);+const clientFactory = createClientFactory()+ // A middleware that is attached first, will be invoked last.+ .use(authInterceptorMiddleware)+ .use(errorNotificationMiddleware);+/**+ * Example to use error notification middleware.+ * Errors occurs during all requests will cause UI notifications automatically.+ * abcServiceClient.foo(requestParams, {+ * // true if you want to suppress error notifications for this call+ * silent: true,+ * })+ */export const authServiceClient = clientFactory.create(AuthServiceDefinition,diff --git a/frontend/src/grpcweb/middlewares/errorNotificationMiddleware.ts b/frontend/src/grpcweb/middlewares/errorNotificationMiddleware.tsnew file mode 100644index 0000000000..034fd6486b--- /dev/null+++ b/frontend/src/grpcweb/middlewares/errorNotificationMiddleware.ts@@ -0,0 +1,71 @@+import { ClientError, ServerError } from "nice-grpc-common";++import { pushNotification } from "@/store";+import { t } from "@/plugins/i18n";+import { ClientMiddleware } from "nice-grpc-web";++export type SilentRequestOptions = {+ /**+ * if set to true, will NOT show push notifications when request error occurs.+ */+ silent?: boolean;+};++/**+ * Way to define a grpc-web middleware+ * ClientMiddleware<CallOptionsExt = {}, RequiredCallOptionsExt = {}>+ * See+ * - https://github.com/deeplay-io/nice-grpc/blob/master/packages/nice-grpc-client-middleware-deadline/src/index.ts+ * - https://github.com/deeplay-io/nice-grpc/tree/master/packages/nice-grpc-web#middleware+ * as an example.+ */+export const errorNotificationMiddleware: ClientMiddleware<SilentRequestOptions> =+ async function* (call, options) {+ const maybePushNotification = (title: string, description?: string) => {+ if (options.silent) return;+ pushNotification({+ module: "bytebase",+ style: "CRITICAL",+ title,+ description,+ });+ };++ const handleError = async (error: unknown) => {+ if (error instanceof ClientError || error instanceof ServerError) {+ maybePushNotification(+ `Code ${error.code}: ${error.message}`,+ error.details+ );+ } else {+ // Other non-grpc errors.+ // E.g,. failed to encode protobuf for request data.+ // or other frontend exception.+ // Expect not to be here.+ maybePushNotification(+ `${t("common.error")}: ${call.method.path}`,+ String(error)+ );+ }+ throw error;+ };++ if (!call.responseStream) {+ try {+ const response = yield* call.next(call.request, options);+ return response;+ } catch (error) {+ await handleError(error);+ }+ } else {+ try {+ for await (const response of call.next(call.request, options)) {+ yield response;+ }+ } catch (error) {+ await handleError(error);+ }++ return;+ }+ };diff --git a/frontend/src/grpcweb/middlewares/index.ts b/frontend/src/grpcweb/middlewares/index.tsindex d46db769e6..a18a2a95ab 100644--- a/frontend/src/grpcweb/middlewares/index.ts+++ b/frontend/src/grpcweb/middlewares/index.ts@@ -1 +1,2 @@export * from "./authInterceptorMiddleware";+export * from "./errorNotificationMiddleware";diff --git a/frontend/src/store/modules/v1/policy.ts b/frontend/src/store/modules/v1/policy.tsindex 4b2078aed5..776838b508 100644--- a/frontend/src/store/modules/v1/policy.ts+++ b/frontend/src/store/modules/v1/policy.ts@@ -109,9 +109,14 @@ export const usePolicyV1Store = defineStore("policy_v1", {return cachedData;}try {- const policy = await policyServiceClient.getPolicy({- name: name.toLowerCase(),- });+ const policy = await policyServiceClient.getPolicy(+ {+ name: name.toLowerCase(),+ },+ {+ silent: true,+ }+ );this.policyMapByName.set(policy.name, policy);return policy;} catch {