Skip to content

Commit

Permalink
Merge pull request #871 from ydb-platform/xerrors
Browse files Browse the repository at this point in the history
allowed to create xerrors.TransportError() from grpc status error + r…
  • Loading branch information
asmyasnikov authored Oct 23, 2023
2 parents 461b0fe + 7a02231 commit 2ea6388
Show file tree
Hide file tree
Showing 13 changed files with 1,177 additions and 190 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Bumped `golang.org/x/sync` to `v0.3.0`
* Bumped `google.golang.org/protobuf` to `v1.31.0`
* Bumped `google.golang.org/grpc` to `v1.57.1`
* Allowed grpc status error as arg in `internal/xerrors.TransportError(err)`
* Added `interanl/xtest.CurrentFileLine()` helper for table tests
* Added `internal/credentials.IsAccessError(err)` helper for check access errors
* Changed period for re-fresh static credentials token from `1/2` to `1/10` to expiration time

Expand Down
38 changes: 20 additions & 18 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ require (
github.com/ydb-platform/ydb-go-sdk-prometheus v0.11.10
github.com/ydb-platform/ydb-go-sdk/v3 v3.47.3
github.com/ydb-platform/ydb-go-yc v0.10.1
google.golang.org/genproto v0.0.0-20230131230820-1c016267d619
google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54
gorm.io/driver/postgres v1.5.0
gorm.io/driver/sqlite v1.5.0
gorm.io/gorm v1.25.1
modernc.org/sqlite v1.14.2
modernc.org/sqlite v1.18.1
xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978
xorm.io/xorm v1.3.2
)
Expand All @@ -25,10 +25,10 @@ require (
cloud.google.com/go/firestore v1.9.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/goccy/go-json v0.8.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand All @@ -43,7 +43,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
Expand All @@ -53,22 +53,24 @@ require (
github.com/ydb-platform/ydb-go-sdk-metrics v0.16.3 // indirect
github.com/ydb-platform/ydb-go-yc-metadata v0.5.4 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
lukechampine.com/uint128 v1.1.1 // indirect
modernc.org/cc/v3 v3.35.18 // indirect
modernc.org/ccgo/v3 v3.12.82 // indirect
modernc.org/libc v1.11.87 // indirect
modernc.org/mathutil v1.4.1 // indirect
modernc.org/memory v1.0.5 // indirect
modernc.org/opt v0.1.1 // indirect
modernc.org/strutil v1.1.1 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.36.3 // indirect
modernc.org/ccgo/v3 v3.16.9 // indirect
modernc.org/libc v1.17.1 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.2.1 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/strutil v1.1.3 // indirect
modernc.org/token v1.0.0 // indirect
)

Expand Down
428 changes: 406 additions & 22 deletions examples/go.sum

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ require (
github.com/google/uuid v1.3.0
github.com/jonboulle/clockwork v0.3.0
github.com/ydb-platform/ydb-go-genproto v0.0.0-20231012155159-f85a672542fd
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
golang.org/x/sync v0.3.0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
google.golang.org/grpc v1.57.1
google.golang.org/protobuf v1.31.0
)

// requires for tests only
Expand All @@ -22,11 +22,11 @@ require (

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
19 changes: 10 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
Expand Down Expand Up @@ -96,8 +97,8 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -132,17 +133,17 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f h1:BWUVssLB0HVOSY78gIdvk1dTVYtT1y8SBWtPYuTJ/6w=
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 h1:0nDDozoAU19Qb2HwhXadU8OcsiO/09cnTqhUtq2MEOM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0=
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
google.golang.org/grpc v1.47.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk=
google.golang.org/grpc v1.53.0 h1:LAv2ds7cmFV/XTS3XG1NneeENYrXGmorPxsBbptIjNc=
google.golang.org/grpc v1.53.0/go.mod h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw=
google.golang.org/grpc v1.57.1 h1:upNTNqv0ES+2ZOOqACwVtS3Il8M12/+Hz41RCPzAjQg=
google.golang.org/grpc v1.57.1/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand All @@ -156,8 +157,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
79 changes: 65 additions & 14 deletions internal/xerrors/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,72 @@ import (
)

func TestIsOperationError(t *testing.T) {
for _, code := range [...]Ydb.StatusIds_StatusCode{
Ydb.StatusIds_BAD_REQUEST,
Ydb.StatusIds_BAD_SESSION,
for _, tt := range []struct {
err error
codes []Ydb.StatusIds_StatusCode
match bool
}{
// check only operation error with any ydb status code
{
err: &operationError{code: Ydb.StatusIds_BAD_REQUEST},
match: true,
},
{
err: fmt.Errorf("wrapped: %w", &operationError{code: Ydb.StatusIds_BAD_REQUEST}),
match: true,
},
{
err: Join(
fmt.Errorf("test"),
&operationError{code: Ydb.StatusIds_BAD_REQUEST},
Retryable(fmt.Errorf("test")),
),
match: true,
},
// match ydb status code
{
err: &operationError{code: Ydb.StatusIds_BAD_REQUEST},
codes: []Ydb.StatusIds_StatusCode{Ydb.StatusIds_BAD_REQUEST},
match: true,
},
{
err: fmt.Errorf("wrapped: %w", &operationError{code: Ydb.StatusIds_BAD_REQUEST}),
codes: []Ydb.StatusIds_StatusCode{Ydb.StatusIds_BAD_REQUEST},
match: true,
},
{
err: Join(
fmt.Errorf("test"),
&operationError{code: Ydb.StatusIds_BAD_REQUEST},
Retryable(fmt.Errorf("test")),
),
codes: []Ydb.StatusIds_StatusCode{Ydb.StatusIds_BAD_REQUEST},
match: true,
},
// no match ydb status code
{
err: &operationError{code: Ydb.StatusIds_BAD_REQUEST},
codes: []Ydb.StatusIds_StatusCode{Ydb.StatusIds_ABORTED},
match: false,
},
{
err: fmt.Errorf("wrapped: %w", &operationError{code: Ydb.StatusIds_BAD_REQUEST}),
codes: []Ydb.StatusIds_StatusCode{Ydb.StatusIds_ABORTED},
match: false,
},
{
err: Join(
fmt.Errorf("test"),
&operationError{code: Ydb.StatusIds_BAD_REQUEST},
Retryable(fmt.Errorf("test")),
),
codes: []Ydb.StatusIds_StatusCode{Ydb.StatusIds_ABORTED},
match: false,
},
} {
for _, err := range []error{
&operationError{code: code},
Operation(WithStatusCode(code)),
fmt.Errorf("wrapped: %w", &operationError{code: code}),
} {
t.Run("", func(t *testing.T) {
if !IsOperationError(err, code) {
t.Errorf("expected %v to be operationError with code=%v", err, code)
}
})
}
t.Run("", func(t *testing.T) {
require.Equal(t, tt.match, IsOperationError(tt.err, tt.codes...))
})
}
}

Expand Down
106 changes: 106 additions & 0 deletions internal/xerrors/pessimized_error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package xerrors

import (
"context"
"errors"
"fmt"
"testing"

grpcCodes "google.golang.org/grpc/codes"
grpcStatus "google.golang.org/grpc/status"
)

func TestMustPessimizeEndpoint(t *testing.T) {
for _, test := range []struct {
error error
pessimize bool
}{
{
error: Transport(grpcStatus.Error(grpcCodes.Canceled, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.Unknown, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.InvalidArgument, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.DeadlineExceeded, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.NotFound, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.AlreadyExists, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.PermissionDenied, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.ResourceExhausted, "")),
pessimize: false,
},
{
error: Transport(grpcStatus.Error(grpcCodes.FailedPrecondition, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.Aborted, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.OutOfRange, "")),
pessimize: false,
},
{
error: Transport(grpcStatus.Error(grpcCodes.Unimplemented, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.Internal, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.Unavailable, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.DataLoss, "")),
pessimize: true,
},
{
error: Transport(grpcStatus.Error(grpcCodes.Unauthenticated, "")),
pessimize: true,
},
{
error: context.Canceled,
pessimize: false,
},
{
error: context.DeadlineExceeded,
pessimize: false,
},
{
error: fmt.Errorf("user error"),
pessimize: false,
},
} {
err := errors.Unwrap(test.error)
if err == nil {
err = test.error
}
t.Run(err.Error(), func(t *testing.T) {
pessimize := MustPessimizeEndpoint(test.error)
if pessimize != test.pessimize {
t.Errorf("unexpected pessimization status for error `%v`: %t, exp: %t", test.error, pessimize, test.pessimize)
}
})
}
}
Loading

0 comments on commit 2ea6388

Please sign in to comment.