如何使用Client在golang中设置Grpc状态和代码

问题描述 投票:0回答:1
camera, err := db.ClientDB.GetCamera(ctx, in.CameraId)     if err != nil {      fmt.Println("err", err)         if errors.Is(err, errors.New("camera not found")) {             return nil, status.Error(codes.NotFound, "Camera not found")         }         return nil, status.Error(codes.Internal, "Problem getting camera apps")     }
message GetCameraApplicationsResponse {   vision.core.CameraAppCollection camera_apps = 1; }
export async function getCameraApplications(   cameraId: string ): Promise<GetCameraApplicationsResponse> {   try {     var request: GetCameraApplicationsRequest =       new GetCameraApplicationsRequest();     request.cameraId = cameraId;     console.log("request.cameraId", cameraId)     const response = await client.getCameraApplications(request);     console.log({response})     return response;   } catch (error: any) {     console.log({error})     if (error instanceof ConnectError) {       switch (error.code) {         case Code.NotFound:           throw new Error('Camera not found');         case Code.InvalidArgument:           throw new Error('Invalid camera ID');         default:           throw new Error('Error fetching camera applications: ' + error.message);       }     }     throw error;   } }

我正在尝试设置 Grpc 状态和代码,对于无效的cameraId,我得到

{
    "error": {
        "name": "ConnectError",
        "rawMessage": "missing trailer",
        "code": 2,
        "metadata": {},
        "details": [],
        "cause": "missing trailer"
    }
}

为了成功,它工作完美。

go error-handling grpc status
1个回答
0
投票

如果尚未完成,请参阅grpc中使用的状态代码https://grpc.io/docs/guides/status-codes/

© www.soinside.com 2019 - 2024. All rights reserved.