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
704e7e3e94
commit
e56b65ecce
72
grpc.go
72
grpc.go
@ -14,6 +14,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang/protobuf/proto"
|
||||||
"github.com/micro/go-micro/v2/broker"
|
"github.com/micro/go-micro/v2/broker"
|
||||||
"github.com/micro/go-micro/v2/codec"
|
"github.com/micro/go-micro/v2/codec"
|
||||||
"github.com/micro/go-micro/v2/errors"
|
"github.com/micro/go-micro/v2/errors"
|
||||||
@ -375,20 +376,38 @@ func (g *grpcServer) processRequest(stream grpc.ServerStream, service *service,
|
|||||||
|
|
||||||
statusCode := codes.OK
|
statusCode := codes.OK
|
||||||
statusDesc := ""
|
statusDesc := ""
|
||||||
|
|
||||||
// execute the handler
|
// execute the handler
|
||||||
if appErr := fn(ctx, r, replyv.Interface()); appErr != nil {
|
if appErr := fn(ctx, r, replyv.Interface()); appErr != nil {
|
||||||
if err, ok := appErr.(*rpcError); ok {
|
var errStatus *status.Status
|
||||||
statusCode = err.code
|
switch verr := appErr.(type) {
|
||||||
statusDesc = err.desc
|
case *errors.Error:
|
||||||
} else if err, ok := appErr.(*errors.Error); ok {
|
// micro.Error now proto based and we can attach it to grpc status
|
||||||
statusCode = microError(err)
|
statusCode = microError(verr)
|
||||||
statusDesc = appErr.Error()
|
statusDesc = verr.Error()
|
||||||
} else {
|
errStatus, err = status.New(statusCode, statusDesc).WithDetails(verr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case proto.Message:
|
||||||
|
// user defined error that proto based we can attach it to grpc status
|
||||||
statusCode = convertCode(appErr)
|
statusCode = convertCode(appErr)
|
||||||
statusDesc = appErr.Error()
|
statusDesc = appErr.Error()
|
||||||
|
errStatus, err = status.New(statusCode, statusDesc).WithDetails(verr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case *rpcError:
|
||||||
|
// rpcError handling may be we have ability to attach it to details?
|
||||||
|
statusCode = verr.code
|
||||||
|
statusDesc = verr.desc
|
||||||
|
errStatus = status.New(statusCode, statusDesc)
|
||||||
|
default:
|
||||||
|
// default case user pass own error type that not proto based
|
||||||
|
statusCode = convertCode(verr)
|
||||||
|
statusDesc = verr.Error()
|
||||||
|
errStatus = status.New(statusCode, statusDesc)
|
||||||
}
|
}
|
||||||
return status.New(statusCode, statusDesc).Err()
|
return errStatus.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := stream.SendMsg(replyv.Interface()); err != nil {
|
if err := stream.SendMsg(replyv.Interface()); err != nil {
|
||||||
@ -436,16 +455,37 @@ func (g *grpcServer) processStream(stream grpc.ServerStream, service *service, m
|
|||||||
|
|
||||||
appErr := fn(ctx, r, ss)
|
appErr := fn(ctx, r, ss)
|
||||||
if appErr != nil {
|
if appErr != nil {
|
||||||
if err, ok := appErr.(*rpcError); ok {
|
var err error
|
||||||
statusCode = err.code
|
var errStatus *status.Status
|
||||||
statusDesc = err.desc
|
switch verr := appErr.(type) {
|
||||||
} else if err, ok := appErr.(*errors.Error); ok {
|
case *errors.Error:
|
||||||
statusCode = microError(err)
|
// micro.Error now proto based and we can attach it to grpc status
|
||||||
statusDesc = appErr.Error()
|
statusCode = microError(verr)
|
||||||
} else {
|
statusDesc = verr.Error()
|
||||||
|
errStatus, err = status.New(statusCode, statusDesc).WithDetails(verr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case proto.Message:
|
||||||
|
// user defined error that proto based we can attach it to grpc status
|
||||||
statusCode = convertCode(appErr)
|
statusCode = convertCode(appErr)
|
||||||
statusDesc = appErr.Error()
|
statusDesc = appErr.Error()
|
||||||
|
errStatus, err = status.New(statusCode, statusDesc).WithDetails(verr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case *rpcError:
|
||||||
|
// rpcError handling may be we have ability to attach it to details?
|
||||||
|
statusCode = verr.code
|
||||||
|
statusDesc = verr.desc
|
||||||
|
errStatus = status.New(statusCode, statusDesc)
|
||||||
|
default:
|
||||||
|
// default case user pass own error type that not proto based
|
||||||
|
statusCode = convertCode(verr)
|
||||||
|
statusDesc = verr.Error()
|
||||||
|
errStatus = status.New(statusCode, statusDesc)
|
||||||
}
|
}
|
||||||
|
return errStatus.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
return status.New(statusCode, statusDesc).Err()
|
return status.New(statusCode, statusDesc).Err()
|
||||||
|
23
grpc_test.go
23
grpc_test.go
@ -4,9 +4,11 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/micro/go-micro/v2/errors"
|
||||||
"github.com/micro/go-micro/v2/registry/memory"
|
"github.com/micro/go-micro/v2/registry/memory"
|
||||||
"github.com/micro/go-micro/v2/server"
|
"github.com/micro/go-micro/v2/server"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
|
||||||
pb "github.com/micro/go-micro/v2/server/grpc/proto"
|
pb "github.com/micro/go-micro/v2/server/grpc/proto"
|
||||||
)
|
)
|
||||||
@ -16,6 +18,10 @@ type testServer struct{}
|
|||||||
|
|
||||||
// TestHello implements helloworld.GreeterServer
|
// TestHello implements helloworld.GreeterServer
|
||||||
func (s *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
func (s *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
||||||
|
if req.Name == "Error" {
|
||||||
|
return &errors.Error{Id: "1", Code: 99, Detail: "detail"}
|
||||||
|
}
|
||||||
|
|
||||||
rsp.Msg = "Hello " + req.Name
|
rsp.Msg = "Hello " + req.Name
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -63,4 +69,21 @@ func TestGRPCServer(t *testing.T) {
|
|||||||
t.Fatalf("Got unexpected response %v", rsp.Msg)
|
t.Fatalf("Got unexpected response %v", rsp.Msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test grpc error
|
||||||
|
rsp := pb.Response{}
|
||||||
|
|
||||||
|
if err := cc.Invoke(context.Background(), "/test.Test/Call", &pb.Request{Name: "Error"}, &rsp); err != nil {
|
||||||
|
st, ok := status.FromError(err)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("invalid error received %#+v\n", err)
|
||||||
|
}
|
||||||
|
verr, ok := st.Details()[0].(*errors.Error)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("invalid error received %#+v\n", st.Details()[0])
|
||||||
|
}
|
||||||
|
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