github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/xerrors/xerrors.go (about) 1 package xerrors 2 3 import ( 4 "context" 5 "errors" 6 "io" 7 8 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb" 9 grpcCodes "google.golang.org/grpc/codes" 10 11 "github.com/ydb-platform/ydb-go-sdk/v3/internal/backoff" 12 ) 13 14 // Error is an interface of error which reports about error code and error name. 15 type Error interface { 16 error 17 18 Code() int32 19 Name() string 20 Type() Type 21 BackoffType() backoff.Type 22 MustDeleteSession() bool 23 } 24 25 func IsTimeoutError(err error) bool { 26 switch { 27 case 28 IsOperationError( 29 err, 30 Ydb.StatusIds_TIMEOUT, 31 Ydb.StatusIds_CANCELLED, 32 ), 33 IsTransportError(err, grpcCodes.Canceled, grpcCodes.DeadlineExceeded), 34 Is( 35 err, 36 context.DeadlineExceeded, 37 context.Canceled, 38 ): 39 return true 40 default: 41 return false 42 } 43 } 44 45 func ErrIf(cond bool, err error) error { 46 if cond { 47 return err 48 } 49 50 return nil 51 } 52 53 func HideEOF(err error) error { 54 if err == nil { 55 return nil 56 } 57 if errors.Is(err, io.EOF) { 58 return nil 59 } 60 61 return err 62 } 63 64 // As is a proxy to errors.As 65 // This need to single import errors 66 func As(err error, targets ...interface{}) bool { 67 if err == nil { 68 panic("nil err") 69 } 70 if len(targets) == 0 { 71 panic("empty targets") 72 } 73 for _, t := range targets { 74 if errors.As(err, t) { 75 return true 76 } 77 } 78 79 return false 80 } 81 82 // IsErrorFromServer return true if err returned from server 83 // (opposite to raised internally in sdk) 84 func IsErrorFromServer(err error) bool { 85 return IsTransportError(err) || IsOperationError(err) 86 } 87 88 // Is is a improved proxy to errors.Is 89 // This need to single import errors 90 func Is(err error, targets ...error) bool { 91 if len(targets) == 0 { 92 panic("empty targets") 93 } 94 for _, target := range targets { 95 if errors.Is(err, target) { 96 return true 97 } 98 } 99 100 return false 101 }