github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/xerrors/ydb_test.go (about)

     1  package xerrors
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
     9  	grpcCodes "google.golang.org/grpc/codes"
    10  	grpcStatus "google.golang.org/grpc/status"
    11  )
    12  
    13  func TestIsYdb(t *testing.T) {
    14  	for _, test := range []struct {
    15  		error      error
    16  		isYdbError bool
    17  	}{
    18  		{
    19  			error:      nil,
    20  			isYdbError: false,
    21  		},
    22  		{
    23  			error:      Operation(WithStatusCode(Ydb.StatusIds_BAD_SESSION)),
    24  			isYdbError: true,
    25  		},
    26  		{
    27  			error:      Transport(grpcStatus.Error(grpcCodes.DeadlineExceeded, "")),
    28  			isYdbError: true,
    29  		},
    30  		{
    31  			error:      RetryableError(fmt.Errorf("")),
    32  			isYdbError: false,
    33  		},
    34  		{
    35  			error:      WithStackTrace(Operation(WithStatusCode(Ydb.StatusIds_BAD_SESSION))),
    36  			isYdbError: true,
    37  		},
    38  		{
    39  			error:      WithStackTrace(Transport(grpcStatus.Error(grpcCodes.DeadlineExceeded, ""))),
    40  			isYdbError: true,
    41  		},
    42  		{
    43  			error:      WithStackTrace(RetryableError(fmt.Errorf(""))),
    44  			isYdbError: false,
    45  		},
    46  		{
    47  			error:      WithStackTrace(WithStackTrace(Operation(WithStatusCode(Ydb.StatusIds_BAD_SESSION)))),
    48  			isYdbError: true,
    49  		},
    50  		{
    51  			error:      WithStackTrace(WithStackTrace(Transport(grpcStatus.Error(grpcCodes.DeadlineExceeded, "")))),
    52  			isYdbError: true,
    53  		},
    54  		{
    55  			error:      WithStackTrace(WithStackTrace(RetryableError(fmt.Errorf("")))),
    56  			isYdbError: false,
    57  		},
    58  		{
    59  			error:      fmt.Errorf("TestError%s", "Printf"),
    60  			isYdbError: false,
    61  		},
    62  		{
    63  			error:      errors.New("TestError"),
    64  			isYdbError: false,
    65  		},
    66  		{
    67  			error:      Wrap(fmt.Errorf("TestError%s", "Printf")),
    68  			isYdbError: true,
    69  		},
    70  		{
    71  			error:      Wrap(errors.New("TestError")),
    72  			isYdbError: true,
    73  		},
    74  	} {
    75  		t.Run("", func(t *testing.T) {
    76  			if IsYdb(test.error) != test.isYdbError {
    77  				t.Fatalf("unexpected check ydb error: %v, want: %v", IsYdb(test.error), test.isYdbError)
    78  			}
    79  		})
    80  	}
    81  }