Skip to content

Commit

Permalink
Merge pull request #873 from ydb-platform/fix-error-brief
Browse files Browse the repository at this point in the history
fix TestErrorBrief
  • Loading branch information
asmyasnikov authored Oct 23, 2023
2 parents e6cf18a + 821f991 commit e1a1bb5
Showing 1 changed file with 40 additions and 50 deletions.
90 changes: 40 additions & 50 deletions metrics/error_brief_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,53 @@ import (
grpcCodes "google.golang.org/grpc/codes"
grpcStatus "google.golang.org/grpc/status"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
)

func stackRecord() string {
return stack.Record(1,
stack.PackagePath(false),
stack.PackageName(false),
stack.StructName(false),
stack.FunctionName(false),
stack.Lambda(false),
)
}

func TestErrorBrief(t *testing.T) {
for _, tt := range []struct {
name string
err error
brief string
}{
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: nil,
brief: "OK",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: context.Canceled,
brief: "context/Canceled",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(context.Canceled),
brief: "context/Canceled",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: context.DeadlineExceeded,
brief: "context/DeadlineExceeded",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(context.DeadlineExceeded),
brief: "context/DeadlineExceeded",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: fmt.Errorf("test"),
brief: "unknown",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: io.EOF,
brief: "io/EOF",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: &net.OpError{
Op: "write",
Addr: &net.TCPAddr{
Expand All @@ -80,29 +70,29 @@ func TestErrorBrief(t *testing.T) {
brief: "network/write[0.0.0.0:2135](transport/Unavailable)",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Retryable(fmt.Errorf("test")),
brief: "retryable/CUSTOM",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
brief: "retryable/SomeName",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(xerrors.Retryable(fmt.Errorf("test"))),
brief: "retryable/CUSTOM",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(
xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
),
brief: "retryable/SomeName",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(&net.OpError{
Op: "write",
Addr: &net.TCPAddr{
Expand All @@ -114,80 +104,80 @@ func TestErrorBrief(t *testing.T) {
brief: "network/write[0.0.0.0:2135](transport/Unavailable)",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: grpcStatus.Error(grpcCodes.Unavailable, ""),
brief: "transport/Unavailable",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Transport(
grpcStatus.Error(grpcCodes.Unavailable, ""),
),
brief: "transport/Unavailable",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
),
brief: "operation/BAD_REQUEST",
},
// errors with stack trace
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(fmt.Errorf("test")),
brief: "unknown",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(io.EOF),
brief: "io/EOF",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(
grpcStatus.Error(grpcCodes.Unavailable, ""),
),
brief: "transport/Unavailable",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(xerrors.Transport(
grpcStatus.Error(grpcCodes.Unavailable, ""),
)),
brief: "transport/Unavailable",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
)),
brief: "operation/BAD_REQUEST",
},
// joined errors
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(fmt.Errorf("test")),
brief: "unknown",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(
fmt.Errorf("test"),
xerrors.Retryable(fmt.Errorf("test")),
),
brief: "retryable/CUSTOM",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(
fmt.Errorf("test"),
xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
),
brief: "retryable/SomeName",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(
fmt.Errorf("test"),
xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
Expand All @@ -196,75 +186,75 @@ func TestErrorBrief(t *testing.T) {
brief: "transport/Unavailable",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(io.EOF),
brief: "io/EOF",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(
grpcStatus.Error(grpcCodes.Unavailable, ""),
),
brief: "transport/Unavailable",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(xerrors.Transport(
grpcStatus.Error(grpcCodes.Unavailable, ""),
)),
brief: "transport/Unavailable",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
)),
brief: "operation/BAD_REQUEST",
},
// joined errors with stack trace
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(xerrors.WithStackTrace(fmt.Errorf("test"))),
brief: "unknown",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(xerrors.WithStackTrace(io.EOF)),
brief: "io/EOF",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(xerrors.WithStackTrace(xerrors.Transport(
grpcStatus.Error(grpcCodes.Unavailable, ""),
))),
brief: "transport/Unavailable",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(xerrors.WithStackTrace(xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
))),
brief: "operation/BAD_REQUEST",
},
// joined errors (mixed types)
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(
xerrors.WithStackTrace(fmt.Errorf("test")),
xerrors.WithStackTrace(io.EOF),
),
brief: "io/EOF",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(xerrors.Join(
xerrors.WithStackTrace(fmt.Errorf("test")),
xerrors.WithStackTrace(io.EOF),
)),
brief: "io/EOF",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(
io.EOF,
grpcStatus.Error(grpcCodes.Unavailable, ""),
Expand All @@ -275,7 +265,7 @@ func TestErrorBrief(t *testing.T) {
brief: "io/EOF",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(
&net.OpError{
Op: "write",
Expand All @@ -294,7 +284,7 @@ func TestErrorBrief(t *testing.T) {
brief: "io/EOF",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.Join(
grpcStatus.Error(grpcCodes.Unavailable, ""),
xerrors.WithStackTrace(xerrors.Operation(
Expand All @@ -304,7 +294,7 @@ func TestErrorBrief(t *testing.T) {
brief: "transport/Unavailable",
},
{
name: stackRecord(),
name: xtest.CurrentFileLine(),
err: xerrors.WithStackTrace(xerrors.Join(
xerrors.WithStackTrace(xerrors.Transport(
grpcStatus.Error(grpcCodes.Unavailable, ""),
Expand Down

0 comments on commit e1a1bb5

Please sign in to comment.