From 3d8cd2b7f64ed6f421f8733d9b23e5f6d0f33206 Mon Sep 17 00:00:00 2001 From: Huiwen Date: Tue, 19 Sep 2023 21:03:55 +0000 Subject: [PATCH] fixup --- .../gitpod-protocol/src/messaging/error.ts | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/components/gitpod-protocol/src/messaging/error.ts b/components/gitpod-protocol/src/messaging/error.ts index 685b56c4671f18..f489c065dbf9f1 100644 --- a/components/gitpod-protocol/src/messaging/error.ts +++ b/components/gitpod-protocol/src/messaging/error.ts @@ -5,7 +5,8 @@ */ import { scrubber } from "../util/scrubbing"; -import * as grpc from "@grpc/grpc-js"; +import { ServerErrorResponse } from "@grpc/grpc-js/build/src/server-call"; +import { Status } from "@grpc/grpc-js/build/src/constants"; export class ApplicationError extends Error { constructor(public readonly code: ErrorCode, message: string, public readonly data?: any) { @@ -38,26 +39,26 @@ export namespace ApplicationError { } } - export function fromGRPCError(e: grpc.ServerErrorResponse, data?: any): ApplicationError { - return new ApplicationError(categorizeRPCError(e), e.message, data); + export function fromGRPCError(e: ServerErrorResponse, data?: any): ApplicationError { + return new ApplicationError(categorizeRPCError(e.code), e.message, data); } - export function categorizeRPCError(e: grpc.ServerErrorResponse): ErrorCode { + export function categorizeRPCError(code?: Status): ErrorCode { // Mostly align to https://github.com/gitpod-io/gitpod/blob/ef95e6f3ca0bf314c40da1b83251423c2208d175/components/public-api-server/pkg/proxy/errors.go#L25 - switch (e.code) { - case grpc.status.INVALID_ARGUMENT: + switch (code) { + case Status.INVALID_ARGUMENT: return ErrorCodes.BAD_REQUEST; - case grpc.status.UNAUTHENTICATED: + case Status.UNAUTHENTICATED: return ErrorCodes.NOT_AUTHENTICATED; - case grpc.status.PERMISSION_DENIED: + case Status.PERMISSION_DENIED: return ErrorCodes.PERMISSION_DENIED; // or UserBlocked - case grpc.status.NOT_FOUND: + case Status.NOT_FOUND: return ErrorCodes.NOT_FOUND; - case grpc.status.ALREADY_EXISTS: + case Status.ALREADY_EXISTS: return ErrorCodes.CONFLICT; - case grpc.status.FAILED_PRECONDITION: + case Status.FAILED_PRECONDITION: return ErrorCodes.PRECONDITION_FAILED; - case grpc.status.RESOURCE_EXHAUSTED: + case Status.RESOURCE_EXHAUSTED: return ErrorCodes.TOO_MANY_REQUESTS; } return ErrorCodes.INTERNAL_SERVER_ERROR;