Skip to content

Commit 55aa1c0

Browse files
committed
added simple wrapper for grpc errors for getting nodeID and address
1 parent e45a77b commit 55aa1c0

File tree

6 files changed

+72
-7
lines changed

6 files changed

+72
-7
lines changed

internal/conn/conn.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func invoke(
380380
defer onTransportError(ctx, err)
381381

382382
if !useWrapping {
383-
return opID, issues, err
383+
return opID, issues, withConnInfo(err, nodeID, address)
384384
}
385385

386386
if sentMark.canRetry() {
@@ -545,7 +545,7 @@ func (c *conn) NewStream(
545545
}()
546546

547547
if !useWrapping {
548-
return nil, err
548+
return nil, withConnInfo(err, c.NodeID(), c.Address())
549549
}
550550

551551
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
@@ -58,7 +58,7 @@ func (s *grpcClientStream) CloseSend() (err error) {
5858
}
5959

6060
if !s.wrapping {
61-
return err
61+
return withConnInfo(err, s.parentConn.NodeID(), s.parentConn.Address())
6262
}
6363

6464
return xerrors.WithStackTrace(xerrors.Transport(
@@ -97,7 +97,7 @@ func (s *grpcClientStream) SendMsg(m interface{}) (err error) {
9797
}()
9898

9999
if !s.wrapping {
100-
return err
100+
return withConnInfo(err, s.parentConn.NodeID(), s.parentConn.Address())
101101
}
102102

103103
if s.sentMark.canRetry() {
@@ -156,7 +156,7 @@ func (s *grpcClientStream) RecvMsg(m interface{}) (err error) { //nolint:funlen
156156
}()
157157

158158
if !s.wrapping {
159-
return err
159+
return withConnInfo(err, s.parentConn.NodeID(), s.parentConn.Address())
160160
}
161161

162162
if s.sentMark.canRetry() {

internal/xerrors/operation.go

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ func (e *operationError) Code() int32 {
2828
return int32(e.code)
2929
}
3030

31+
func (e *operationError) NodeID() uint32 {
32+
return e.nodeID
33+
}
34+
35+
func (e *operationError) Address() string {
36+
return e.address
37+
}
38+
3139
func (e *operationError) Name() string {
3240
return "operation/" + e.code.String()
3341
}

internal/xerrors/transport.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ func (e *transportError) GRPCStatus() *grpcStatus.Status {
2727

2828
func (e *transportError) isYdbError() {}
2929

30+
func (e *transportError) NodeID() uint32 {
31+
return e.nodeID
32+
}
33+
34+
func (e *transportError) Address() string {
35+
return e.address
36+
}
37+
3038
func (e *transportError) Code() int32 {
3139
return int32(e.status.Code())
3240
}
@@ -134,8 +142,8 @@ func IsTransportError(err error, codes ...grpcCodes.Code) bool {
134142
var status *grpcStatus.Status
135143
if t := (*transportError)(nil); errors.As(err, &t) {
136144
status = t.status
137-
} else if t, has := grpcStatus.FromError(err); has {
138-
status = t
145+
} else if s, has := grpcStatus.FromError(err); has {
146+
status = s
139147
}
140148
if status != nil {
141149
if len(codes) == 0 {

0 commit comments

Comments
 (0)