Skip to content

Commit 4c3662a

Browse files
committed
added simple wrapper for grpc errors for getting nodeID and address
1 parent c78681e commit 4c3662a

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

internal/conn/conn.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func invoke(
365365
defer onTransportError(ctx, err)
366366

367367
if !useWrapping {
368-
return opID, issues, err
368+
return opID, issues, withConnInfo(err, nodeID, address)
369369
}
370370

371371
if sentMark.canRetry() {
@@ -530,7 +530,7 @@ func (c *conn) NewStream(
530530
}()
531531

532532
if !useWrapping {
533-
return nil, err
533+
return nil, withConnInfo(err, c.NodeID(), c.Address())
534534
}
535535

536536
if sentMark.canRetry() {

internal/conn/errors.go

+31
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,34 @@ func IsBadConn(err error, goodConnCodes ...grpcCodes.Code) bool {
3838

3939
return true
4040
}
41+
42+
type grpcError struct {
43+
err error
44+
45+
nodeID uint32
46+
address string
47+
}
48+
49+
func (e *grpcError) Error() string {
50+
return e.err.Error()
51+
}
52+
53+
func (e *grpcError) As(target any) bool {
54+
return xerrors.As(e.err, target)
55+
}
56+
57+
func (e *grpcError) NodeID() uint32 {
58+
return e.nodeID
59+
}
60+
61+
func (e *grpcError) Address() string {
62+
return e.address
63+
}
64+
65+
func withConnInfo(err error, nodeID uint32, address string) error {
66+
return &grpcError{
67+
err: err,
68+
nodeID: nodeID,
69+
address: address,
70+
}
71+
}

internal/conn/errors_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,21 @@ func TestIsBadConn(t *testing.T) {
108108
})
109109
}
110110
}
111+
112+
func TestGrpcError(t *testing.T) {
113+
err := withConnInfo(grpcStatus.Error(grpcCodes.Unavailable, "test"), 123, "test:123")
114+
require.Equal(t, `rpc error: code = Unavailable desc = test`, err.Error())
115+
var nodeID interface {
116+
NodeID() uint32
117+
}
118+
require.ErrorAs(t, err, &nodeID)
119+
require.Equal(t, uint32(123), nodeID.NodeID())
120+
var address interface {
121+
Address() string
122+
}
123+
require.ErrorAs(t, err, &address)
124+
require.Equal(t, "test:123", address.Address())
125+
s, has := grpcStatus.FromError(err)
126+
require.True(t, has)
127+
require.Equal(t, grpcCodes.Unavailable, s.Code())
128+
}

internal/conn/grpc_client_stream.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (s *grpcClientStream) CloseSend() (err error) {
5959
}
6060

6161
if !s.wrapping {
62-
return err
62+
return withConnInfo(err, s.parentConn.NodeID(), s.parentConn.Address())
6363
}
6464

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

101101
if !s.wrapping {
102-
return err
102+
return withConnInfo(err, s.parentConn.NodeID(), s.parentConn.Address())
103103
}
104104

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

161161
if !s.wrapping {
162-
return err
162+
return withConnInfo(err, s.parentConn.NodeID(), s.parentConn.Address())
163163
}
164164

165165
if s.sentMark.canRetry() {

internal/xerrors/transport.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ func IsTransportError(err error, codes ...grpcCodes.Code) bool {
134134
var status *grpcStatus.Status
135135
if t := (*transportError)(nil); errors.As(err, &t) {
136136
status = t.status
137-
} else if t, has := grpcStatus.FromError(err); has {
138-
status = t
137+
} else if s, has := grpcStatus.FromError(err); has {
138+
status = s
139139
}
140140
if status != nil {
141141
if len(codes) == 0 {

0 commit comments

Comments
 (0)