Skip to content

Commit

Permalink
Merge pull request berty#2949 from berty/revert-2920-chore/improve-gr…
Browse files Browse the repository at this point in the history
…pc-bridge-error

fix(messenger): revert error details bug
  • Loading branch information
n0izn0iz authored Dec 23, 2020
2 parents 1d118fb + ccfcdfd commit b437554
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 275 deletions.
5 changes: 0 additions & 5 deletions api/bertyaccount.proto
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,9 @@ message Metadata {
}

message Error {
// grpc error code
GRPCErrCode grpc_error_code = 1;
// toplevel error
errcode.ErrCode error_code = 2;
// toplevel error message
string message = 3;
// detailed errors
errcode.ErrDetails error_details = 4;
}

enum GRPCErrCode {
Expand Down
2 changes: 1 addition & 1 deletion docs/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions go/internal/grpcutil/lazy_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ func NewLazyClient(cc *grpc.ClientConn) *LazyClient {
return &LazyClient{cc: cc}
}

func (lc *LazyClient) InvokeUnary(ctx context.Context, desc *LazyMethodDesc, in *LazyMessage, copts ...grpc.CallOption) (out *LazyMessage, err error) {
func (lc *LazyClient) InvokeUnary(ctx context.Context, desc *LazyMethodDesc, in *LazyMessage) (out *LazyMessage, err error) {
out = NewLazyMessage()
err = grpc.Invoke(ctx, desc.Name, in, out, lc.cc, append(copts, grpc.ForceCodec(lazyCodec))...)
err = grpc.Invoke(ctx, desc.Name, in, out, lc.cc, grpc.ForceCodec(lazyCodec))
return
}

func (lc *LazyClient) InvokeStream(ctx context.Context, desc *LazyMethodDesc, in *LazyMessage, copts ...grpc.CallOption) (*LazyStream, error) {
func (lc *LazyClient) InvokeStream(ctx context.Context, desc *LazyMethodDesc, in *LazyMessage) (*LazyStream, error) {
sdesc := &grpc.StreamDesc{
StreamName: desc.Name,
ServerStreams: desc.ServerStreams,
ClientStreams: desc.ClientStreams,
}

sctx, cancel := context.WithCancel(ctx)
cstream, err := grpc.NewClientStream(sctx, sdesc, lc.cc, desc.Name, append(copts, grpc.ForceCodec(lazyCodec))...)
cstream, err := grpc.NewClientStream(sctx, sdesc, lc.cc, desc.Name, grpc.ForceCodec(lazyCodec))
if err != nil {
cancel()
return nil, err
Expand Down
199 changes: 93 additions & 106 deletions go/pkg/bertyaccount/bertyaccount.pb.go

Large diffs are not rendered by default.

53 changes: 31 additions & 22 deletions go/pkg/bertyaccount/service_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

ma "github.com/multiformats/go-multiaddr"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -40,16 +39,19 @@ func (s *service) ClientInvokeUnary(ctx context.Context, req *ClientInvokeUnary_
}

// create fake proto message
trailer := metadata.MD{}
in := grpcutil.NewLazyMessage().FromBytes(req.Payload)
out, err := serviceClient.InvokeUnary(uctx, desc, in, grpc.Trailer(&trailer))
res.Error = getServiceError(err)
out, err := serviceClient.InvokeUnary(ctx, desc, in)
if err != nil {
res.Error = getServiceError(err)
return res, nil
}

res.Payload = out.Bytes()
res.Trailer = convertMetadata(trailer)
res.Trailer = getMetadataFromContext(uctx)
return res, nil
}

// CreateStream create a stream
// // CreateStream create a stream
func (s *service) CreateClientStream(ctx context.Context, req *ClientCreateStream_Request) (*ClientCreateStream_Reply, error) {
serviceClient, err := s.getServiceClient()
if err != nil {
Expand Down Expand Up @@ -96,11 +98,10 @@ func (s *service) ClientStreamSend(ctx context.Context, req *ClientStreamSend_Re
res := &ClientStreamSend_Reply{StreamId: id}

in := grpcutil.NewLazyMessage().FromBytes(req.Payload)
err = cstream.SendMsg(in)
res.Error = getServiceError(err)

if err != nil {
if err := cstream.SendMsg(in); err != nil {
res.Error = getServiceError(err)
res.Trailer = convertMetadata(cstream.Trailer())

s.muStreams.Lock()
delete(s.streams, id)
s.muStreams.Unlock()
Expand Down Expand Up @@ -200,6 +201,15 @@ func newOutgoingContext(ctx context.Context, md []*Metadata) context.Context {
return metadata.NewOutgoingContext(ctx, outmd)
}

func getMetadataFromContext(ctx context.Context) []*Metadata {
// get trailer
if outmd, ok := metadata.FromIncomingContext(ctx); ok {
return convertMetadata(outmd)
}

return nil
}

func convertMetadata(in metadata.MD) []*Metadata {
out := make([]*Metadata, in.Len())
i := 0
Expand All @@ -216,24 +226,25 @@ func convertMetadata(in metadata.MD) []*Metadata {

func getServiceError(err error) *Error {
if err == nil {
return &Error{}
return nil
}

grpcErrCode := GRPCErrCode_OK
var errCode errcode.ErrCode
var grpcErrCode GRPCErrCode

if s := status.Convert(err); s.Code() != codes.OK {
grpcErrCode = GRPCErrCode(s.Code())
}

errCode := errcode.Undefined
errCodes := errcode.Codes(err)
if len(errCodes) > 0 {
errCode = errCodes[0]
if code := errcode.Code(err); code > 0 {
errCode = code
} else {
errCode = errcode.Undefined
}

return &Error{
GrpcErrorCode: grpcErrCode,
ErrorCode: errCode,
ErrorDetails: &errcode.ErrDetails{Codes: errCodes},
Message: err.Error(),
}
}
Expand All @@ -242,15 +253,13 @@ func (s *service) clientStreamRecv(id string, cstream *grpcutil.LazyStream) *Cli
res := &ClientStreamRecv_Reply{StreamId: id}

out := grpcutil.NewLazyMessage()
err := cstream.RecvMsg(out)
res.Error = getServiceError(err)
if err := cstream.RecvMsg(out); err != nil {
s.muStreams.Lock()

if err != nil {
res.Error = getServiceError(err)
res.Trailer = convertMetadata(cstream.Trailer())

s.muStreams.Lock()
delete(s.streams, id)

s.muStreams.Unlock()
return res
}
Expand Down
129 changes: 0 additions & 129 deletions go/pkg/bertyaccount/service_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion js/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions js/packages/api/root.pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions js/packages/api/root.pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b437554

Please sign in to comment.