github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/metrics/error_brief_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"net"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
    12  	grpcCodes "google.golang.org/grpc/codes"
    13  	grpcStatus "google.golang.org/grpc/status"
    14  
    15  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    16  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
    17  )
    18  
    19  func TestErrorBrief(t *testing.T) {
    20  	for _, tt := range []struct {
    21  		name  string
    22  		err   error
    23  		brief string
    24  	}{
    25  		{
    26  			name:  xtest.CurrentFileLine(),
    27  			err:   nil,
    28  			brief: "OK",
    29  		},
    30  		{
    31  			name:  xtest.CurrentFileLine(),
    32  			err:   context.Canceled,
    33  			brief: "context/Canceled",
    34  		},
    35  		{
    36  			name:  xtest.CurrentFileLine(),
    37  			err:   xerrors.WithStackTrace(context.Canceled),
    38  			brief: "context/Canceled",
    39  		},
    40  		{
    41  			name:  xtest.CurrentFileLine(),
    42  			err:   context.DeadlineExceeded,
    43  			brief: "context/DeadlineExceeded",
    44  		},
    45  		{
    46  			name:  xtest.CurrentFileLine(),
    47  			err:   xerrors.WithStackTrace(context.DeadlineExceeded),
    48  			brief: "context/DeadlineExceeded",
    49  		},
    50  		{
    51  			name:  xtest.CurrentFileLine(),
    52  			err:   fmt.Errorf("test"),
    53  			brief: "unknown",
    54  		},
    55  		{
    56  			name:  xtest.CurrentFileLine(),
    57  			err:   io.EOF,
    58  			brief: "io/EOF",
    59  		},
    60  		{
    61  			name: xtest.CurrentFileLine(),
    62  			err: &net.OpError{
    63  				Op: "write",
    64  				Addr: &net.TCPAddr{
    65  					IP:   []byte{0, 0, 0, 0},
    66  					Port: 2135,
    67  				},
    68  				Err: grpcStatus.Error(grpcCodes.Unavailable, ""),
    69  			},
    70  			brief: "network/write[0.0.0.0:2135](transport/Unavailable)",
    71  		},
    72  		{
    73  			name:  xtest.CurrentFileLine(),
    74  			err:   xerrors.Retryable(fmt.Errorf("test")),
    75  			brief: "retryable/CUSTOM",
    76  		},
    77  		{
    78  			name:  xtest.CurrentFileLine(),
    79  			err:   xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
    80  			brief: "retryable/SomeName",
    81  		},
    82  		{
    83  			name:  xtest.CurrentFileLine(),
    84  			err:   xerrors.WithStackTrace(xerrors.Retryable(fmt.Errorf("test"))),
    85  			brief: "retryable/CUSTOM",
    86  		},
    87  		{
    88  			name: xtest.CurrentFileLine(),
    89  			err: xerrors.WithStackTrace(
    90  				xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
    91  			),
    92  			brief: "retryable/SomeName",
    93  		},
    94  		{
    95  			name: xtest.CurrentFileLine(),
    96  			err: xerrors.WithStackTrace(&net.OpError{
    97  				Op: "write",
    98  				Addr: &net.TCPAddr{
    99  					IP:   []byte{0, 0, 0, 0},
   100  					Port: 2135,
   101  				},
   102  				Err: grpcStatus.Error(grpcCodes.Unavailable, ""),
   103  			}),
   104  			brief: "network/write[0.0.0.0:2135](transport/Unavailable)",
   105  		},
   106  		{
   107  			name:  xtest.CurrentFileLine(),
   108  			err:   grpcStatus.Error(grpcCodes.Unavailable, ""),
   109  			brief: "transport/Unavailable",
   110  		},
   111  		{
   112  			name: xtest.CurrentFileLine(),
   113  			err: xerrors.Transport(
   114  				grpcStatus.Error(grpcCodes.Unavailable, ""),
   115  			),
   116  			brief: "transport/Unavailable",
   117  		},
   118  		{
   119  			name: xtest.CurrentFileLine(),
   120  			err: xerrors.Operation(
   121  				xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
   122  			),
   123  			brief: "operation/BAD_REQUEST",
   124  		},
   125  		// errors with stack trace
   126  		{
   127  			name:  xtest.CurrentFileLine(),
   128  			err:   xerrors.WithStackTrace(fmt.Errorf("test")),
   129  			brief: "unknown",
   130  		},
   131  		{
   132  			name:  xtest.CurrentFileLine(),
   133  			err:   xerrors.WithStackTrace(io.EOF),
   134  			brief: "io/EOF",
   135  		},
   136  		{
   137  			name: xtest.CurrentFileLine(),
   138  			err: xerrors.WithStackTrace(
   139  				grpcStatus.Error(grpcCodes.Unavailable, ""),
   140  			),
   141  			brief: "transport/Unavailable",
   142  		},
   143  		{
   144  			name: xtest.CurrentFileLine(),
   145  			err: xerrors.WithStackTrace(xerrors.Transport(
   146  				grpcStatus.Error(grpcCodes.Unavailable, ""),
   147  			)),
   148  			brief: "transport/Unavailable",
   149  		},
   150  		{
   151  			name: xtest.CurrentFileLine(),
   152  			err: xerrors.WithStackTrace(xerrors.Operation(
   153  				xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
   154  			)),
   155  			brief: "operation/BAD_REQUEST",
   156  		},
   157  		// joined errors
   158  		{
   159  			name:  xtest.CurrentFileLine(),
   160  			err:   xerrors.Join(fmt.Errorf("test")),
   161  			brief: "unknown",
   162  		},
   163  		{
   164  			name: xtest.CurrentFileLine(),
   165  			err: xerrors.Join(
   166  				fmt.Errorf("test"),
   167  				xerrors.Retryable(fmt.Errorf("test")),
   168  			),
   169  			brief: "retryable/CUSTOM",
   170  		},
   171  		{
   172  			name: xtest.CurrentFileLine(),
   173  			err: xerrors.Join(
   174  				fmt.Errorf("test"),
   175  				xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
   176  			),
   177  			brief: "retryable/SomeName",
   178  		},
   179  		{
   180  			name: xtest.CurrentFileLine(),
   181  			err: xerrors.Join(
   182  				fmt.Errorf("test"),
   183  				xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
   184  				grpcStatus.Error(grpcCodes.Unavailable, "test"),
   185  			),
   186  			brief: "transport/Unavailable",
   187  		},
   188  		{
   189  			name:  xtest.CurrentFileLine(),
   190  			err:   xerrors.Join(io.EOF),
   191  			brief: "io/EOF",
   192  		},
   193  		{
   194  			name: xtest.CurrentFileLine(),
   195  			err: xerrors.Join(
   196  				grpcStatus.Error(grpcCodes.Unavailable, ""),
   197  			),
   198  			brief: "transport/Unavailable",
   199  		},
   200  		{
   201  			name: xtest.CurrentFileLine(),
   202  			err: xerrors.Join(xerrors.Transport(
   203  				grpcStatus.Error(grpcCodes.Unavailable, ""),
   204  			)),
   205  			brief: "transport/Unavailable",
   206  		},
   207  		{
   208  			name: xtest.CurrentFileLine(),
   209  			err: xerrors.Join(xerrors.Operation(
   210  				xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
   211  			)),
   212  			brief: "operation/BAD_REQUEST",
   213  		},
   214  		// joined errors with stack trace
   215  		{
   216  			name:  xtest.CurrentFileLine(),
   217  			err:   xerrors.Join(xerrors.WithStackTrace(fmt.Errorf("test"))),
   218  			brief: "unknown",
   219  		},
   220  		{
   221  			name:  xtest.CurrentFileLine(),
   222  			err:   xerrors.Join(xerrors.WithStackTrace(io.EOF)),
   223  			brief: "io/EOF",
   224  		},
   225  		{
   226  			name: xtest.CurrentFileLine(),
   227  			err: xerrors.Join(xerrors.WithStackTrace(xerrors.Transport(
   228  				grpcStatus.Error(grpcCodes.Unavailable, ""),
   229  			))),
   230  			brief: "transport/Unavailable",
   231  		},
   232  		{
   233  			name: xtest.CurrentFileLine(),
   234  			err: xerrors.Join(xerrors.WithStackTrace(xerrors.Operation(
   235  				xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
   236  			))),
   237  			brief: "operation/BAD_REQUEST",
   238  		},
   239  		// joined errors (mixed types)
   240  		{
   241  			name: xtest.CurrentFileLine(),
   242  			err: xerrors.Join(
   243  				xerrors.WithStackTrace(fmt.Errorf("test")),
   244  				xerrors.WithStackTrace(io.EOF),
   245  			),
   246  			brief: "io/EOF",
   247  		},
   248  		{
   249  			name: xtest.CurrentFileLine(),
   250  			err: xerrors.WithStackTrace(xerrors.Join(
   251  				xerrors.WithStackTrace(fmt.Errorf("test")),
   252  				xerrors.WithStackTrace(io.EOF),
   253  			)),
   254  			brief: "io/EOF",
   255  		},
   256  		{
   257  			name: xtest.CurrentFileLine(),
   258  			err: xerrors.Join(
   259  				io.EOF,
   260  				grpcStatus.Error(grpcCodes.Unavailable, ""),
   261  				xerrors.WithStackTrace(xerrors.Operation(
   262  					xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
   263  				)),
   264  			),
   265  			brief: "io/EOF",
   266  		},
   267  		{
   268  			name: xtest.CurrentFileLine(),
   269  			err: xerrors.Join(
   270  				&net.OpError{
   271  					Op: "write",
   272  					Addr: &net.TCPAddr{
   273  						IP:   []byte{0, 0, 0, 0},
   274  						Port: 2135,
   275  					},
   276  					Err: grpcStatus.Error(grpcCodes.Unavailable, ""),
   277  				},
   278  				io.EOF,
   279  				grpcStatus.Error(grpcCodes.Unavailable, ""),
   280  				xerrors.WithStackTrace(xerrors.Operation(
   281  					xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
   282  				)),
   283  			),
   284  			brief: "io/EOF",
   285  		},
   286  		{
   287  			name: xtest.CurrentFileLine(),
   288  			err: xerrors.Join(
   289  				grpcStatus.Error(grpcCodes.Unavailable, ""),
   290  				xerrors.WithStackTrace(xerrors.Operation(
   291  					xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
   292  				)),
   293  			),
   294  			brief: "transport/Unavailable",
   295  		},
   296  		{
   297  			name: xtest.CurrentFileLine(),
   298  			err: xerrors.WithStackTrace(xerrors.Join(
   299  				xerrors.WithStackTrace(xerrors.Transport(
   300  					grpcStatus.Error(grpcCodes.Unavailable, ""),
   301  				)),
   302  				xerrors.WithStackTrace(xerrors.Operation(
   303  					xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
   304  				)),
   305  			)),
   306  			brief: "transport/Unavailable",
   307  		},
   308  	} {
   309  		t.Run(tt.name, func(t *testing.T) {
   310  			require.Equal(t, tt.brief, errorBrief(tt.err))
   311  		})
   312  	}
   313  }