亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 grpc 的狀態錯誤對象的詳細信息中發送自定義 proto

如何在 grpc 的狀態錯誤對象的詳細信息中發送自定義 proto

Go
桃花長相依 2022-06-01 18:13:38
我正在嘗試在我的 grpc 調用的錯誤響應中發送自定義原型。按照這個,應該是可以的。這是代碼。    st := status.Newf(codes.NotFound, "user %s doesn't exist", req.Name)    desc := "The username Doesn't exist, please give a valid username"    customProtoError := &data.Error{        Message:         "Check username",        Code:            1404,        Type:            "Service",        DetailedMessage: desc,    }    st, err := st.WithDetails(customProtoError)    if err != nil {        panic(fmt.Sprintf("Unexpected error attaching metadata: %v", err))    }我有這些 GRPC API 的 grpc 和 http 處理程序。HTTP 服務器中使用的 Marshaler 是github.com/gogo/gatewaymux := runtime.NewServeMux(    runtime.WithMarshalerOption(JSONContentType, &gateway.JSONPb{}))HTTP和GRPC服務器的配置可在此處獲得。當我嘗試使用 HTTP 調用訪問 API 時,這是我得到的響應。HTTP 錯誤:{    "error": "user kishore1 doesn't exist",    "code": 5,    "message": "user kishore1 doesn't exist",    "details": [        {            "type_url": "type.googleapis.com/data.Error",            "value": "Cg5DaGVjayB1c2VybmFtZRD8ChoHU2t5ZmxvdyI4VGhlIHVzZXJuYW1lIERvZXNuJ3QgZXhpc3QsIHBsZWFzZSBnaXZlIGEgdmFsaWQgdXNlcm5hbWU="        }    ]}type_url與 google 一起提供,因為它在golang proto code中進行了硬編碼。但這在 JSON 中表示 Any將被反序列化為嵌入式消息。GRPC 客戶端代碼:resp, err := client.GetUser(ctx, req)        if err != nil {        st := status.Convert(err)        for _, detail := range st.Details() {            switch t := detail.(type) {            case *pb.Error:                fmt.Println("Oops! Your request was rejected by the server.")                ...            default:                fmt.Println("Error Received - ", detail, reflect.TypeOf(detail))            }        }grpc錯誤:Error Received - proto: not found *errors.prefixError知道是什么導致了這種混亂嗎?HTTP 不會反序列化為嵌入式消息,而 GRPC 提供proto.NotFound這可能是因為 gogo proto 是網關嗎?我什至對 googleapis proto 進行了同樣的嘗試,errdetails.BadRequest但還是一樣。
查看完整描述

1 回答

?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

花了大約一天后,我才發現問題是由于版本不兼容造成的。因此,在使用 gogo proto 時,這對我有用。

對于 GRPC 錯誤,我必須升級我的 grpc-gateway 版本并使用gogo/status而不是grpc/status

github.com/gogo/status v1.1.0
github.com/grpc-ecosystem/grpc-gateway v1.14.5

對于 HTTP 錯誤,我不得不降級我的golang/protobuf

github.com/golang/protobuf v1.3.2


查看完整回答
反對 回復 2022-06-01
  • 1 回答
  • 0 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號