pass micro errors from grpc server to grpc client (#1167)
* pass micro errors from grpc server to grpc client Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * wrap micro errors.Error to grpc status Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
8740815c56
commit
a5a238d554
16
error.go
16
error.go
@ -12,17 +12,21 @@ func microError(err error) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// micro error
|
if verr, ok := err.(*errors.Error); ok {
|
||||||
if v, ok := err.(*errors.Error); ok {
|
return verr
|
||||||
return v
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// grpc error
|
// grpc error
|
||||||
if s, ok := status.FromError(err); ok {
|
if s, ok := status.FromError(err); ok {
|
||||||
if e := errors.Parse(s.Message()); e.Code > 0 {
|
details := s.Details()
|
||||||
return e // actually a micro error
|
if len(details) == 0 {
|
||||||
|
if e := errors.Parse(s.Message()); e.Code > 0 {
|
||||||
|
return e // actually a micro error
|
||||||
|
}
|
||||||
|
return errors.InternalServerError("go.micro.client", s.Message())
|
||||||
}
|
}
|
||||||
return errors.InternalServerError("go.micro.client", s.Message())
|
// return first error from details
|
||||||
|
return details[0].(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// do nothing
|
// do nothing
|
||||||
|
25
grpc_test.go
25
grpc_test.go
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/micro/go-micro/v2/client"
|
"github.com/micro/go-micro/v2/client"
|
||||||
"github.com/micro/go-micro/v2/client/selector"
|
"github.com/micro/go-micro/v2/client/selector"
|
||||||
|
"github.com/micro/go-micro/v2/errors"
|
||||||
"github.com/micro/go-micro/v2/registry"
|
"github.com/micro/go-micro/v2/registry"
|
||||||
"github.com/micro/go-micro/v2/registry/memory"
|
"github.com/micro/go-micro/v2/registry/memory"
|
||||||
pgrpc "google.golang.org/grpc"
|
pgrpc "google.golang.org/grpc"
|
||||||
@ -18,6 +19,9 @@ type greeterServer struct{}
|
|||||||
|
|
||||||
// SayHello implements helloworld.GreeterServer
|
// SayHello implements helloworld.GreeterServer
|
||||||
func (g *greeterServer) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
|
func (g *greeterServer) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
|
||||||
|
if in.Name == "Error" {
|
||||||
|
return nil, &errors.Error{Id: "1", Code: 99, Detail: "detail"}
|
||||||
|
}
|
||||||
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
|
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,4 +88,25 @@ func TestGRPCClient(t *testing.T) {
|
|||||||
t.Fatalf("Got unexpected response %v", rsp.Message)
|
t.Fatalf("Got unexpected response %v", rsp.Message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req := c.NewRequest("helloworld", "/helloworld.Greeter/SayHello", &pb.HelloRequest{
|
||||||
|
Name: "Error",
|
||||||
|
})
|
||||||
|
|
||||||
|
rsp := pb.HelloReply{}
|
||||||
|
|
||||||
|
err = c.Call(context.TODO(), req, &rsp)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("nil error received")
|
||||||
|
}
|
||||||
|
|
||||||
|
verr, ok := err.(*errors.Error)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("invalid error received %#+v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if verr.Code != 99 && verr.Id != "1" && verr.Detail != "detail" {
|
||||||
|
t.Fatalf("invalid error received %#+v\n", verr)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user