Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(fe): connect rpc clients interceptors (#16581)

p0nyab16eb04dabb
diff --git a/frontend/src/grpcweb/context-key/ignored_codes.ts b/frontend/src/grpcweb/context-key/ignored_codes.ts
new file mode 100644
index 0000000000..f7d7ae5ce6
--- /dev/null
+++ b/frontend/src/grpcweb/context-key/ignored_codes.ts
@@ -0,0 +1,5 @@
+import { Code, createContextKey } from "@connectrpc/connect";
+
+export const ignoredCodesContextKey = createContextKey<Code[]>([], {
+ description: "ignored status codes",
+});
\ No newline at end of file
diff --git a/frontend/src/grpcweb/context-key/index.ts b/frontend/src/grpcweb/context-key/index.ts
new file mode 100644
index 0000000000..ae17ec410a
--- /dev/null
+++ b/frontend/src/grpcweb/context-key/index.ts
@@ -0,0 +1,2 @@
+export { silentContextKey } from "./silent";
+export { ignoredCodesContextKey } from "./ignored_codes";
\ No newline at end of file
diff --git a/frontend/src/grpcweb/context-key/silent.ts b/frontend/src/grpcweb/context-key/silent.ts
new file mode 100644
index 0000000000..2dd7e283d0
--- /dev/null
+++ b/frontend/src/grpcweb/context-key/silent.ts
@@ -0,0 +1,5 @@
+import { createContextKey } from "@connectrpc/connect";
+
+export const silentContextKey = createContextKey<boolean>(false, {
+ description: "silent mode, won't redirect nor pop notifications",
+});
\ No newline at end of file
diff --git a/frontend/src/grpcweb/index.ts b/frontend/src/grpcweb/index.ts
index 7e50e2fa80..827f6dc047 100644
--- a/frontend/src/grpcweb/index.ts
+++ b/frontend/src/grpcweb/index.ts
@@ -38,6 +38,8 @@ import { WorksheetServiceDefinition } from "@/types/proto/v1/worksheet_service";
import { WorkspaceServiceDefinition } from "@/types/proto/v1/workspace_service";
import {
authInterceptorMiddleware,
+ authInterceptor,
+ errorNotificationInterceptor,
errorNotificationMiddleware,
simulateLatencyMiddleware,
} from "./middlewares";
@@ -230,6 +232,7 @@ export const instanceRoleServiceClient = clientFactory.create(
const transport = createConnectTransport({
baseUrl: address,
useBinaryFormat: true,
+ interceptors: [authInterceptor, errorNotificationInterceptor],
fetch: (input, init) => fetch(input, { ...init, credentials: "include" }),
});
@@ -237,4 +240,3 @@ export const actuatorServiceClientConnect = createClient(
ActuatorService,
transport
);
-
diff --git a/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts b/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts
index e46006d83b..d21f03b6fd 100644
--- a/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts
+++ b/frontend/src/grpcweb/middlewares/authInterceptorMiddleware.ts
@@ -1,8 +1,11 @@
+import { Code, ConnectError, type Interceptor } from "@connectrpc/connect";
import { ClientError, ServerError, Status } from "nice-grpc-common";
import type { ClientMiddleware } from "nice-grpc-web";
import { router } from "@/router";
import { useAuthStore } from "@/store";
+import { UserService } from "@/types/proto-es/v1/user_service_pb";
import { UserServiceDefinition } from "@/types/proto/v1/user_service";
+import { silentContextKey, ignoredCodesContextKey } from "../context-key";
export type IgnoreErrorsOptions = {
/**
@@ -75,3 +78,39 @@ export const authInterceptorMiddleware: ClientMiddleware<IgnoreErrorsOptions> =
return;
}
};
+
+export const authInterceptor: Interceptor = (next) => async (req) => {
+ try {
+ const resp = await next(req);
+ return resp;
+ } catch (error) {
+ // If silent is set to true, will NOT show redirect to other pages(e.g., 403, sign in page).
+ const silent = req.contextValues.get(silentContextKey);
+ const ignoredCodes = req.contextValues.get(ignoredCodesContextKey);
+
+ if (!silent && error instanceof ConnectError) {
+ const { code } = error;
+ if (ignoredCodes.includes(code)) {
+ // omit specified errors
+ } else {
+ if (code === Code.Unauthenticated) {
+ // Skip show login modal when the request is to get current user.
+ if (
+ req.method.parent.name === UserService.name &&
+ req.method.name === UserService.method.getCurrentUser.name
+ ) {
+ // skip
+ } else {
+ // When receiving 401 and is returned by our server, it means the current
+ // login user's token becomes invalid. Thus we force the user to login again.
+ useAuthStore().unauthenticatedOccurred = true;
+ }
+ } else if (code === Code.PermissionDenied) {
+ // Jump to 403 page
+ router.push({ name: "error.403" });
+ }
+ }
+ }
+ throw error;
+ }
+};
diff --git a/frontend/src/grpcweb/middlewares/errorNotificationMiddleware.ts b/frontend/src/grpcweb/middlewares/errorNotificationMiddleware.ts
index 53114e0fab..059be9b9d9 100644
--- a/frontend/src/grpcweb/middlewares/errorNotificationMiddleware.ts
+++ b/frontend/src/grpcweb/middlewares/errorNotificationMiddleware.ts
@@ -1,7 +1,9 @@
+import { Code, ConnectError, type Interceptor } from "@connectrpc/connect";
import { ClientError, ServerError, Status } from "nice-grpc-common";
import type { ClientMiddleware } from "nice-grpc-web";
import { t } from "@/plugins/i18n";
import { pushNotification } from "@/store";
+import { ignoredCodesContextKey, silentContextKey } from "../context-key";
export type SilentRequestOptions = {
/**
@@ -84,3 +86,52 @@ export const errorNotificationMiddleware: ClientMiddleware<SilentRequestOptions>
return;
}
};
+
+export const errorNotificationInterceptor: Interceptor =
+ (next) => async (req) => {
+ try {
+ const resp = await next(req);
+ return resp;
+ } catch (error) {
+ const silent = req.contextValues.get(silentContextKey);
+ const ignoredCodes = req.contextValues.get(ignoredCodesContextKey);
+ const maybePushNotification = (title: string, description?: string) => {
+ if (silent) return;
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title,
+ description,
+ });
+ };
+ if (error instanceof ConnectError) {
+ if (
+ (ignoredCodes.length === 0
+ ? [Code.NotFound, Code.Unauthenticated]
+ : ignoredCodes
+ ).includes(error.code)
+ ) {
+ // ignored
+ } else {
+ const details = [error.message];
+ if (error.code === Code.Unknown) {
+ details.push("The backend server may be unavailable");
+ }
+ maybePushNotification(
+ `Code ${error.code}: ${Status[error.code]}`,
+ details.join("\n")
+ );
+ }
+ } 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")}: ${req.service.name}/${req.method.name}`,
+ String(error)
+ );
+ }
+ throw error;
+ }
+ };