Skip to content

Commit da9f66b

Browse files
committed
fix
1 parent 55aa1c0 commit da9f66b

File tree

5 files changed

+48
-38
lines changed

5 files changed

+48
-38
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Added nodeID and address to operation and transport errors
2+
13
## v3.99.4
24
* Fixed bug with wrong context on session closing
35
* Fixed goroutine leak on closing `database/sql` driver

internal/conn/errors.go

+1-28
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,6 @@ func IsBadConn(err error, goodConnCodes ...grpcCodes.Code) bool {
3939
return true
4040
}
4141

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-
6542
func withConnInfo(err error, nodeID uint32, address string) error {
66-
return &grpcError{
67-
err: err,
68-
nodeID: nodeID,
69-
address: address,
70-
}
43+
return xerrors.Transport(err, xerrors.WithNodeID(nodeID), xerrors.WithAddress(address))
7144
}

internal/conn/errors_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,19 @@ func TestIsBadConn(t *testing.T) {
109109
}
110110
}
111111

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())
112+
func TestWithConnInfo(t *testing.T) {
113+
err := withConnInfo(grpcStatus.Error(grpcCodes.Unavailable, "test"), 100500, "example.com:2135")
114+
require.ErrorIs(t, err, grpcStatus.Error(grpcCodes.Unavailable, "test"))
115115
var nodeID interface {
116116
NodeID() uint32
117117
}
118118
require.ErrorAs(t, err, &nodeID)
119-
require.Equal(t, uint32(123), nodeID.NodeID())
119+
require.Equal(t, uint32(100500), nodeID.NodeID())
120120
var address interface {
121121
Address() string
122122
}
123123
require.ErrorAs(t, err, &address)
124-
require.Equal(t, "test:123", address.Address())
124+
require.Equal(t, "example.com:2135", address.Address())
125125
s, has := grpcStatus.FromError(err)
126126
require.True(t, has)
127127
require.Equal(t, grpcCodes.Unavailable, s.Code())

internal/xerrors/operation_test.go

+30-3
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,30 @@ import (
77
"github.com/stretchr/testify/require"
88
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
99
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Issue"
10+
11+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
1012
)
1113

1214
func TestIsOperationError(t *testing.T) {
1315
for _, tt := range []struct {
16+
name string
1417
err error
1518
codes []Ydb.StatusIds_StatusCode
1619
match bool
1720
}{
1821
// check only operation error with any ydb status code
1922
{
23+
name: xtest.CurrentFileLine(),
2024
err: &operationError{code: Ydb.StatusIds_BAD_REQUEST},
2125
match: true,
2226
},
2327
{
28+
name: xtest.CurrentFileLine(),
2429
err: fmt.Errorf("wrapped: %w", &operationError{code: Ydb.StatusIds_BAD_REQUEST}),
2530
match: true,
2631
},
2732
{
33+
name: xtest.CurrentFileLine(),
2834
err: Join(
2935
fmt.Errorf("test"),
3036
&operationError{code: Ydb.StatusIds_BAD_REQUEST},
@@ -34,16 +40,19 @@ func TestIsOperationError(t *testing.T) {
3440
},
3541
// match ydb status code
3642
{
43+
name: xtest.CurrentFileLine(),
3744
err: &operationError{code: Ydb.StatusIds_BAD_REQUEST},
3845
codes: []Ydb.StatusIds_StatusCode{Ydb.StatusIds_BAD_REQUEST},
3946
match: true,
4047
},
4148
{
49+
name: xtest.CurrentFileLine(),
4250
err: fmt.Errorf("wrapped: %w", &operationError{code: Ydb.StatusIds_BAD_REQUEST}),
4351
codes: []Ydb.StatusIds_StatusCode{Ydb.StatusIds_BAD_REQUEST},
4452
match: true,
4553
},
4654
{
55+
name: xtest.CurrentFileLine(),
4756
err: Join(
4857
fmt.Errorf("test"),
4958
&operationError{code: Ydb.StatusIds_BAD_REQUEST},
@@ -54,16 +63,19 @@ func TestIsOperationError(t *testing.T) {
5463
},
5564
// no match ydb status code
5665
{
66+
name: xtest.CurrentFileLine(),
5767
err: &operationError{code: Ydb.StatusIds_BAD_REQUEST},
5868
codes: []Ydb.StatusIds_StatusCode{Ydb.StatusIds_ABORTED},
5969
match: false,
6070
},
6171
{
72+
name: xtest.CurrentFileLine(),
6273
err: fmt.Errorf("wrapped: %w", &operationError{code: Ydb.StatusIds_BAD_REQUEST}),
6374
codes: []Ydb.StatusIds_StatusCode{Ydb.StatusIds_ABORTED},
6475
match: false,
6576
},
6677
{
78+
name: xtest.CurrentFileLine(),
6779
err: Join(
6880
fmt.Errorf("test"),
6981
&operationError{code: Ydb.StatusIds_BAD_REQUEST},
@@ -73,18 +85,20 @@ func TestIsOperationError(t *testing.T) {
7385
match: false,
7486
},
7587
} {
76-
t.Run("", func(t *testing.T) {
88+
t.Run(tt.name, func(t *testing.T) {
7789
require.Equal(t, tt.match, IsOperationError(tt.err, tt.codes...))
7890
})
7991
}
8092
}
8193

8294
func TestIsOperationErrorTransactionLocksInvalidated(t *testing.T) {
8395
for _, tt := range [...]struct {
96+
name string
8497
err error
8598
isTLI bool
8699
}{
87100
{
101+
name: xtest.CurrentFileLine(),
88102
err: Operation(
89103
WithStatusCode(Ydb.StatusIds_ABORTED),
90104
WithIssues([]*Ydb_Issue.IssueMessage{{
@@ -94,6 +108,7 @@ func TestIsOperationErrorTransactionLocksInvalidated(t *testing.T) {
94108
isTLI: true,
95109
},
96110
{
111+
name: xtest.CurrentFileLine(),
97112
err: Operation(
98113
WithStatusCode(Ydb.StatusIds_OVERLOADED),
99114
WithIssues([]*Ydb_Issue.IssueMessage{{
@@ -103,12 +118,14 @@ func TestIsOperationErrorTransactionLocksInvalidated(t *testing.T) {
103118
isTLI: false,
104119
},
105120
{
121+
name: xtest.CurrentFileLine(),
106122
err: Operation(
107123
WithStatusCode(Ydb.StatusIds_ABORTED),
108124
),
109125
isTLI: false,
110126
},
111127
{
128+
name: xtest.CurrentFileLine(),
112129
err: Operation(
113130
WithStatusCode(Ydb.StatusIds_ABORTED),
114131
WithIssues([]*Ydb_Issue.IssueMessage{{
@@ -120,30 +137,40 @@ func TestIsOperationErrorTransactionLocksInvalidated(t *testing.T) {
120137
isTLI: true,
121138
},
122139
} {
123-
t.Run("", func(t *testing.T) {
140+
t.Run(tt.name, func(t *testing.T) {
124141
require.Equal(t, tt.isTLI, IsOperationErrorTransactionLocksInvalidated(tt.err))
125142
})
126143
}
127144
}
128145

129146
func Test_operationError_Error(t *testing.T) {
130147
for _, tt := range []struct {
148+
name string
131149
err error
132150
text string
133151
}{
134152
{
153+
name: xtest.CurrentFileLine(),
135154
err: Operation(WithStatusCode(Ydb.StatusIds_BAD_REQUEST), WithAddress("localhost")),
136155
text: "operation/BAD_REQUEST (code = 400010, address = localhost)",
137156
},
138157
{
158+
name: xtest.CurrentFileLine(),
159+
err: Operation(WithStatusCode(Ydb.StatusIds_BAD_REQUEST), WithNodeID(100500)),
160+
text: "operation/BAD_REQUEST (code = 400010, nodeID = 100500)",
161+
},
162+
{
163+
name: xtest.CurrentFileLine(),
139164
err: Operation(WithStatusCode(Ydb.StatusIds_BAD_REQUEST)),
140165
text: "operation/BAD_REQUEST (code = 400010)",
141166
},
142167
{
168+
name: xtest.CurrentFileLine(),
143169
err: Operation(WithStatusCode(Ydb.StatusIds_BAD_SESSION)),
144170
text: "operation/BAD_SESSION (code = 400100)",
145171
},
146172
{
173+
name: xtest.CurrentFileLine(),
147174
err: Operation(WithStatusCode(Ydb.StatusIds_PRECONDITION_FAILED), WithIssues([]*Ydb_Issue.IssueMessage{
148175
{
149176
Message: "issue one",
@@ -177,7 +204,7 @@ func Test_operationError_Error(t *testing.T) {
177204
text: "operation/PRECONDITION_FAILED (code = 400120, issues = [{15:3 => #1 'issue one'},{#2 'issue two' [{test.yql:16:4 => #3 'issue three'},{#4 'issue four'}]}])", //nolint:lll
178205
},
179206
} {
180-
t.Run("", func(t *testing.T) {
207+
t.Run(tt.name, func(t *testing.T) {
181208
require.Equal(t, tt.text, tt.err.Error())
182209
})
183210
}

internal/xerrors/transport_test.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,27 @@ func TestGrpcError(t *testing.T) {
145145

146146
func TestTransportErrorString(t *testing.T) {
147147
for _, tt := range []struct {
148+
name string
148149
err error
149150
text string
150151
}{
151152
{
153+
name: xtest.CurrentFileLine(),
152154
err: Transport(grpcStatus.Error(grpcCodes.FailedPrecondition, "")),
153155
text: "transport/FailedPrecondition (code = 9, source error = \"rpc error: code = FailedPrecondition desc = \")",
154156
},
155157
{
158+
name: xtest.CurrentFileLine(),
156159
err: Transport(grpcStatus.Error(grpcCodes.Unavailable, ""), WithAddress("localhost:2135")),
157160
text: "transport/Unavailable (code = 14, source error = \"rpc error: code = Unavailable desc = \", address: \"localhost:2135\")", //nolint:lll
158161
},
162+
{
163+
name: xtest.CurrentFileLine(),
164+
err: Transport(grpcStatus.Error(grpcCodes.Unavailable, ""), WithNodeID(100500)),
165+
text: "transport/Unavailable (code = 14, source error = \"rpc error: code = Unavailable desc = \", nodeID = 100500)", //nolint:lll
166+
},
159167
} {
160-
t.Run("", func(t *testing.T) {
168+
t.Run(tt.name, func(t *testing.T) {
161169
require.Equal(t, tt.text, tt.err.Error())
162170
})
163171
}
@@ -189,7 +197,7 @@ func TestTransportErrorName(t *testing.T) {
189197
name: "transport/Aborted",
190198
},
191199
} {
192-
t.Run("", func(t *testing.T) {
200+
t.Run(tt.name, func(t *testing.T) {
193201
if tt.err == nil {
194202
require.Nil(t, TransportError(tt.err)) //nolint:testifylint
195203
} else {

0 commit comments

Comments
 (0)