Skip to content

Commit aeee9de

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

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
@@ -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/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)