W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
參考samples dubbo-go-samples/error
用戶可以在 provider 端生成用戶定義的異常信息,可以記錄異常產(chǎn)生堆棧,triple 協(xié)議可保證將用戶在客戶端獲取到異常 message ,并可以查看報錯堆棧,便于定位問題。
注意返回 error 非 nil 時,框架不負(fù)責(zé)其他返回值的傳遞。
package main import ( "context" ) import ( "dubbo.apache.org/dubbo-go/v3/common/logger" // 使用可以記錄對戰(zhàn)信息的異常庫,此處以 "github.com/pkg/errors" 為例 "github.com/pkg/errors" ) import ( triplepb "github.com/apache/dubbo-go-samples/api" ) // 一個實現(xiàn)了 pb 接口的服務(wù)提供結(jié)構(gòu) type ErrorResponseProvider struct { triplepb.UnimplementedGreeterServer } // 回傳錯誤的接口 func (s *ErrorResponseProvider) SayHello(ctx context.Context, in *triplepb.HelloRequest) (*triplepb.User, error) { logger.Infof("Dubbo3 GreeterProvider get user name = %s\n" + in.Name) // 返回用戶自定義異常 return &triplepb.User{Name: "Hello " + in.Name, Id: "12345", Age: 21}, errors.New("user defined error") }
package main import ( "context" ) import ( "dubbo.apache.org/dubbo-go/v3/common/logger" "dubbo.apache.org/dubbo-go/v3/config" _ "dubbo.apache.org/dubbo-go/v3/imports" tripleCommon "github.com/dubbogo/triple/pkg/common" ) import ( triplepb "github.com/apache/dubbo-go-samples/api" ) var greeterProvider = new(triplepb.GreeterClientImpl) func init() { config.SetConsumerService(greeterProvider) } func main() { if err := config.Load(); err != nil { panic(err) } req := triplepb.HelloRequest{ Name: "laurence", } // 發(fā)起調(diào)用 if user, err := greeterProvider.SayHello(context.TODO(), &req); err != nil { // 打印異常信息,err.Error() 將返回用戶定義的 message,即 user defined error logger.Infof("response result: %v, error = %s", user, err) // 打印異常堆棧,需斷言為 tripleCommon.TripleError logger.Infof("error details = %+v", err.(tripleCommon.TripleError).Stacks()) } }
2021-11-12T18:36:33.730+0800 INFO cmd/client.go:53 response result: , error = user defined error 2021-11-12T18:36:33.730+0800 INFO cmd/client.go:54 error details = [type.googleapis.com/google.rpc.DebugInfo]:{stack_entries:"user defined error main.(*ErrorResponseProvider).SayHello /dubbo-go-samples/error/triple/pb/go-server/cmd/error_reponse.go:40 reflect.Value.call /usr/local/go/src/reflect/value.go:543 reflect.Value.Call /usr/local/go/src/reflect/value.go:339 dubbo.apache.org/dubbo-go/v3/common/proxy/proxy_factory.(*ProxyInvoker).Invoke /Users/laurence/go/pkg/mod/dubbo.apache.org/dubbo-go/v3@v3.0.0-rc4-1/common/proxy/proxy_factory/default.go:145 ...
可看到報錯信息和堆棧
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: