Skip to content

Commit

Permalink
added simple wrapper for grpc errors for getting nodeID and address
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Aug 2, 2024
1 parent c78681e commit 4c3662a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
4 changes: 2 additions & 2 deletions internal/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func invoke(
defer onTransportError(ctx, err)

if !useWrapping {
return opID, issues, err
return opID, issues, withConnInfo(err, nodeID, address)
}

if sentMark.canRetry() {
Expand Down Expand Up @@ -530,7 +530,7 @@ func (c *conn) NewStream(
}()

if !useWrapping {
return nil, err
return nil, withConnInfo(err, c.NodeID(), c.Address())
}

if sentMark.canRetry() {
Expand Down
31 changes: 31 additions & 0 deletions internal/conn/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,34 @@ func IsBadConn(err error, goodConnCodes ...grpcCodes.Code) bool {

return true
}

type grpcError struct {
err error

nodeID uint32
address string
}

func (e *grpcError) Error() string {
return e.err.Error()
}

func (e *grpcError) As(target any) bool {
return xerrors.As(e.err, target)
}

func (e *grpcError) NodeID() uint32 {
return e.nodeID
}

func (e *grpcError) Address() string {
return e.address
}

func withConnInfo(err error, nodeID uint32, address string) error {
return &grpcError{
err: err,
nodeID: nodeID,
address: address,
}
}
18 changes: 18 additions & 0 deletions internal/conn/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,21 @@ func TestIsBadConn(t *testing.T) {
})
}
}

func TestGrpcError(t *testing.T) {
err := withConnInfo(grpcStatus.Error(grpcCodes.Unavailable, "test"), 123, "test:123")
require.Equal(t, `rpc error: code = Unavailable desc = test`, err.Error())
var nodeID interface {
NodeID() uint32
}
require.ErrorAs(t, err, &nodeID)
require.Equal(t, uint32(123), nodeID.NodeID())
var address interface {
Address() string
}
require.ErrorAs(t, err, &address)
require.Equal(t, "test:123", address.Address())
s, has := grpcStatus.FromError(err)
require.True(t, has)
require.Equal(t, grpcCodes.Unavailable, s.Code())
}
6 changes: 3 additions & 3 deletions internal/conn/grpc_client_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *grpcClientStream) CloseSend() (err error) {
}

if !s.wrapping {
return err
return withConnInfo(err, s.parentConn.NodeID(), s.parentConn.Address())
}

return xerrors.WithStackTrace(xerrors.Transport(
Expand Down Expand Up @@ -99,7 +99,7 @@ func (s *grpcClientStream) SendMsg(m interface{}) (err error) {
}()

if !s.wrapping {
return err
return withConnInfo(err, s.parentConn.NodeID(), s.parentConn.Address())
}

if s.sentMark.canRetry() {
Expand Down Expand Up @@ -159,7 +159,7 @@ func (s *grpcClientStream) RecvMsg(m interface{}) (err error) { //nolint:funlen
}()

if !s.wrapping {
return err
return withConnInfo(err, s.parentConn.NodeID(), s.parentConn.Address())
}

if s.sentMark.canRetry() {
Expand Down
4 changes: 2 additions & 2 deletions internal/xerrors/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ func IsTransportError(err error, codes ...grpcCodes.Code) bool {
var status *grpcStatus.Status
if t := (*transportError)(nil); errors.As(err, &t) {
status = t.status
} else if t, has := grpcStatus.FromError(err); has {
status = t
} else if s, has := grpcStatus.FromError(err); has {
status = s
}
if status != nil {
if len(codes) == 0 {
Expand Down

0 comments on commit 4c3662a

Please sign in to comment.