Skip to content

Commit 10c46c9

Browse files
committed
* Improved errors.IsTransportError (check a few transport error codes instead check single transport error code)
* Improved `errors.Is` (check a few errors instead check single error) * Refactored YDB errors checking API on client-side
1 parent f0cfca8 commit 10c46c9

File tree

15 files changed

+346
-236
lines changed

15 files changed

+346
-236
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
* Improved error messages
33
* Defended `cluster.balancer` with `sync.RWMutex` on `cluster.Insert`, `cluster.Update`, `cluster.Remove` and `cluster.Get`
44
* Excluded `Close` and `Park` methods from `conn.Conn` interface
5-
* Fixed bug with `Create()` on multi-balancer
5+
* Fixed bug with `Multi` balancer `Create()`
6+
* Improved `errors.IsTransportError` (check a few transport error codes instead check single transport error code)
7+
* Improved `errors.Is` (check a few errors instead check single error)
8+
* Refactored YDB errors checking API on client-side
69

710
## 3.13.0
811
* Refactored `Connection` interface

errors.go

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,24 @@ func IsTimeoutError(err error) bool {
2828
return errors.IsTimeoutError(err)
2929
}
3030

31-
func IsTransportError(err error) bool {
32-
return TransportErrorDescription(err) != nil
33-
}
34-
35-
func IsTransportErrorCode(err error, codes ...int32) bool {
36-
d := TransportErrorDescription(err)
37-
if d == nil {
38-
return false
39-
}
40-
for _, code := range codes {
41-
if d.Code() == code {
42-
return true
43-
}
44-
}
45-
return false
31+
func IsTransportError(err error, codes ...int32) bool {
32+
return errors.IsTransportError(
33+
err,
34+
func() (cc []errors.TransportErrorCode) {
35+
for _, code := range codes {
36+
cc = append(cc, errors.TransportErrorCode(code))
37+
}
38+
return cc
39+
}()...,
40+
)
4641
}
4742

4843
func IsTransportErrorCancelled(err error) bool {
49-
return IsTransportErrorCode(err, int32(errors.TransportErrorCanceled))
44+
return IsTransportError(err, int32(errors.TransportErrorCanceled))
5045
}
5146

5247
func IsTransportErrorResourceExhausted(err error) bool {
53-
return IsTransportErrorCode(err, int32(errors.TransportErrorResourceExhausted))
48+
return IsTransportError(err, int32(errors.TransportErrorResourceExhausted))
5449
}
5550

5651
type Error interface {
@@ -72,21 +67,16 @@ func IsYdbError(err error) bool {
7267
return IsTransportError(err) || IsOperationError(err)
7368
}
7469

75-
func IsOperationError(err error) bool {
76-
return OperationErrorDescription(err) != nil
77-
}
78-
79-
func IsOperationErrorCode(err error, codes ...int32) bool {
80-
d := OperationErrorDescription(err)
81-
if d == nil {
82-
return false
83-
}
84-
for _, code := range codes {
85-
if d.Code() == code {
86-
return true
87-
}
88-
}
89-
return false
70+
func IsOperationError(err error, codes ...int32) bool {
71+
return errors.IsOpError(
72+
err,
73+
func() (cc []errors.StatusCode) {
74+
for _, code := range codes {
75+
cc = append(cc, errors.StatusCode(code))
76+
}
77+
return cc
78+
}()...,
79+
)
9080
}
9181

9282
func OperationErrorDescription(err error) Error {
@@ -98,23 +88,23 @@ func OperationErrorDescription(err error) Error {
9888
}
9989

10090
func IsOperationErrorOverloaded(err error) bool {
101-
return IsOperationErrorCode(err, int32(errors.StatusOverloaded))
91+
return IsOperationError(err, int32(errors.StatusOverloaded))
10292
}
10393

10494
func IsOperationErrorUnavailable(err error) bool {
105-
return IsOperationErrorCode(err, int32(errors.StatusUnavailable))
95+
return IsOperationError(err, int32(errors.StatusUnavailable))
10696
}
10797

10898
func IsOperationErrorAlreadyExistsError(err error) bool {
109-
return IsOperationErrorCode(err, int32(errors.StatusAlreadyExists))
99+
return IsOperationError(err, int32(errors.StatusAlreadyExists))
110100
}
111101

112102
func IsOperationErrorNotFoundError(err error) bool {
113-
return IsOperationErrorCode(err, int32(errors.StatusNotFound))
103+
return IsOperationError(err, int32(errors.StatusNotFound))
114104
}
115105

116106
func IsOperationErrorSchemeError(err error) bool {
117-
return IsOperationErrorCode(err, int32(errors.StatusSchemeError))
107+
return IsOperationError(err, int32(errors.StatusSchemeError))
118108
}
119109

120110
func IsRatelimiterAcquireError(err error) bool {

internal/errors/errors.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ import (
1414
func IsTimeoutError(err error) bool {
1515
switch {
1616
case
17-
IsOpError(err, StatusTimeout),
18-
IsOpError(err, StatusCancelled),
19-
IsTransportError(err, TransportErrorCanceled),
20-
IsTransportError(err, TransportErrorDeadlineExceeded),
21-
errors.Is(err, context.DeadlineExceeded),
22-
errors.Is(err, context.Canceled):
17+
IsOpError(
18+
err,
19+
StatusTimeout,
20+
StatusCancelled,
21+
),
22+
IsTransportError(err, TransportErrorCanceled, TransportErrorDeadlineExceeded),
23+
Is(
24+
err,
25+
context.DeadlineExceeded,
26+
context.Canceled,
27+
):
2328
return true
2429
default:
2530
return false
@@ -49,10 +54,18 @@ func As(err error, target interface{}) bool {
4954
return errors.As(err, target)
5055
}
5156

52-
// Is is a proxy to errors.Is
57+
// Is is a improved proxy to errors.Is
5358
// This need to single import errors
54-
func Is(err, target error) bool {
55-
return errors.Is(err, target)
59+
func Is(err error, targets ...error) bool {
60+
if len(targets) == 0 {
61+
panic("empty targets")
62+
}
63+
for _, target := range targets {
64+
if errors.Is(err, target) {
65+
return true
66+
}
67+
}
68+
return false
5669
}
5770

5871
// New is a proxy to errors.New

internal/errors/operation.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ func IsOpError(err error, codes ...StatusCode) bool {
8989
if !errors.As(err, &op) {
9090
return false
9191
}
92+
if len(codes) == 0 {
93+
return true
94+
}
9295
for _, code := range codes {
9396
if op.Reason == code {
9497
return true

internal/errors/transport.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,23 @@ func transportErrorString(t TransportErrorCode) string {
320320

321321
// IsTransportError reports whether err is TransportError with given code as
322322
// the Reason.
323-
func IsTransportError(err error, code TransportErrorCode) bool {
323+
func IsTransportError(err error, codes ...TransportErrorCode) bool {
324324
if err == nil {
325325
return false
326326
}
327327
var t *TransportError
328328
if !errors.As(err, &t) {
329329
return false
330330
}
331-
return t.Reason == code
331+
if len(codes) == 0 {
332+
return true
333+
}
334+
for _, code := range codes {
335+
if t.Reason == code {
336+
return true
337+
}
338+
}
339+
return false
332340
}
333341

334342
func MapGRPCError(err error) error {

internal/scripting/scripting.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Scripting"
1212
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_TableStats"
1313

14+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/errors"
1415
"github.com/ydb-platform/ydb-go-sdk/v3/internal/operation"
1516
"github.com/ydb-platform/ydb-go-sdk/v3/internal/table/scanner"
1617
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
@@ -134,7 +135,7 @@ func (c *client) StreamExecute(
134135
)
135136
defer func() {
136137
if err != nil {
137-
onIntermediate(err)(err)
138+
onIntermediate(errors.HideEOF(err))(errors.HideEOF(err))
138139
}
139140
}()
140141

@@ -153,7 +154,7 @@ func (c *client) StreamExecute(
153154
err error,
154155
) {
155156
defer func() {
156-
onIntermediate(err)
157+
onIntermediate(errors.HideEOF(err))
157158
}()
158159
select {
159160
case <-ctx.Done():
@@ -169,7 +170,7 @@ func (c *client) StreamExecute(
169170
},
170171
func(err error) error {
171172
cancel()
172-
onIntermediate(err)(err)
173+
onIntermediate(errors.HideEOF(err))(errors.HideEOF(err))
173174
return err
174175
},
175176
), nil

internal/table/client.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func newClient(
5858
builder SessionBuilder,
5959
config config.Config,
6060
) *client {
61-
onDone := trace.TableOnPoolInit(config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
61+
onDone := trace.TableOnInit(config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
6262
if builder == nil {
6363
builder = func(ctx context.Context) (s Session, err error) {
6464
return newSession(ctx, cc, config)
@@ -128,9 +128,12 @@ func isCreateSessionErrorRetriable(err error) bool {
128128
case
129129
errors.Is(err, ErrSessionPoolOverflow),
130130
errors.IsOpError(err, errors.StatusOverloaded),
131-
errors.IsTransportError(err, errors.TransportErrorResourceExhausted),
132-
errors.IsTransportError(err, errors.TransportErrorDeadlineExceeded),
133-
errors.IsTransportError(err, errors.TransportErrorUnavailable):
131+
errors.IsTransportError(
132+
err,
133+
errors.TransportErrorResourceExhausted,
134+
errors.TransportErrorDeadlineExceeded,
135+
errors.TransportErrorUnavailable,
136+
):
134137
return true
135138
default:
136139
return false
@@ -414,7 +417,7 @@ func (c *client) Put(ctx context.Context, s Session) (err error) {
414417
// It returns first error occurred during stale sessions' deletion.
415418
// Note that even on error it calls Close() on each session.
416419
func (c *client) Close(ctx context.Context) (err error) {
417-
onDone := trace.TableOnPoolClose(c.config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
420+
onDone := trace.TableOnClose(c.config.Trace().Compose(trace.ContextTable(ctx)), &ctx)
418421
defer func() {
419422
onDone(err)
420423
}()
@@ -573,11 +576,17 @@ func (c *client) keeper(ctx context.Context) {
573576
if err != nil {
574577
switch {
575578
case
576-
errors.Is(err, cluster.ErrClusterClosed),
577-
errors.Is(err, cluster.ErrClusterEmpty),
579+
errors.Is(
580+
err,
581+
cluster.ErrClusterClosed,
582+
cluster.ErrClusterEmpty,
583+
),
578584
errors.IsOpError(err, errors.StatusBadSession),
579-
errors.IsTransportError(err, errors.TransportErrorDeadlineExceeded),
580-
errors.IsTransportError(err, errors.TransportErrorUnavailable):
585+
errors.IsTransportError(
586+
err,
587+
errors.TransportErrorDeadlineExceeded,
588+
errors.TransportErrorUnavailable,
589+
):
581590
toDelete = append(toDelete, s)
582591
default:
583592
toTryAgain = append(toTryAgain, s)

internal/table/session.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_TableStats"
1818

1919
"github.com/ydb-platform/ydb-go-sdk/v3/internal/cluster"
20+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/errors"
2021
"github.com/ydb-platform/ydb-go-sdk/v3/internal/feature"
2122
"github.com/ydb-platform/ydb-go-sdk/v3/internal/operation"
2223
"github.com/ydb-platform/ydb-go-sdk/v3/internal/table/scanner"
@@ -960,7 +961,7 @@ func (s *session) StreamExecuteScanQuery(
960961
)
961962
defer func() {
962963
if err != nil {
963-
onIntermediate(err)(err)
964+
onIntermediate(errors.HideEOF(err))(errors.HideEOF(err))
964965
}
965966
}()
966967

@@ -987,7 +988,7 @@ func (s *session) StreamExecuteScanQuery(
987988
err error,
988989
) {
989990
defer func() {
990-
onIntermediate(err)
991+
onIntermediate(errors.HideEOF(err))
991992
}()
992993
select {
993994
case <-ctx.Done():
@@ -1003,7 +1004,7 @@ func (s *session) StreamExecuteScanQuery(
10031004
},
10041005
func(err error) error {
10051006
cancel()
1006-
onIntermediate(err)(err)
1007+
onIntermediate(errors.HideEOF(err))(errors.HideEOF(err))
10071008
if checkHintSessionClose(stream.Trailer()) {
10081009
s.SetStatus(options.SessionClosing)
10091010
}

0 commit comments

Comments
 (0)