Skip to content

Commit

Permalink
added TestErrorBrief
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Oct 23, 2023
1 parent cc653eb commit bd8617f
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 7 deletions.
18 changes: 11 additions & 7 deletions metrics/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package metrics

import (
"context"
"errors"
"io"
"net"

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

func errorBrief(err error) string {
Expand All @@ -18,15 +18,19 @@ func errorBrief(err error) string {
ydbErr ydb.Error
)
switch {
case errors.As(err, &netErr):
return "network/" + netErr.Op + " -> " + netErr.Err.Error()
case errors.Is(err, io.EOF):
case xerrors.Is(err, io.EOF):
return "io/EOF"
case errors.Is(err, context.DeadlineExceeded):
case xerrors.As(err, &netErr):
return "network/" + netErr.Op + "[" + netErr.Addr.String() + "](" + errorBrief(netErr.Err) + ")"
case xerrors.Is(err, context.DeadlineExceeded):
return "context/DeadlineExceeded"
case errors.Is(err, context.Canceled):
case xerrors.Is(err, context.Canceled):
return "context/Canceled"
case errors.As(err, &ydbErr):
case xerrors.IsTransportError(err):
return xerrors.TransportError(err).Name()
case xerrors.IsOperationError(err):
return xerrors.OperationError(err).Name()
case xerrors.As(err, &ydbErr):
return ydbErr.Name()
default:
return "unknown"
Expand Down
251 changes: 251 additions & 0 deletions metrics/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
package metrics

import (
"fmt"
"io"
"net"
"testing"

"github.com/stretchr/testify/require"
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
grpcCodes "google.golang.org/grpc/codes"
grpcStatus "google.golang.org/grpc/status"

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

func TestErrorBrief(t *testing.T) {
for _, tt := range []struct {
err error
brief string
}{
{
err: fmt.Errorf("test"),
brief: "unknown",
},
{
err: io.EOF,
brief: "io/EOF",
},
{
err: &net.OpError{
Op: "write",
Addr: &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: 2135,
},
Err: grpcStatus.Error(grpcCodes.Unavailable, ""),
},
brief: "network/write[0.0.0.0:2135](transport/Unavailable)",
},
{
err: xerrors.Retryable(fmt.Errorf("test")),
brief: "retryable/CUSTOM",
},
{
err: xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
brief: "retryable/SomeName",
},
{
err: xerrors.WithStackTrace(xerrors.Retryable(fmt.Errorf("test"))),
brief: "retryable/CUSTOM",
},
{
err: xerrors.WithStackTrace(
xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
),
brief: "retryable/SomeName",
},
{
err: xerrors.WithStackTrace(&net.OpError{
Op: "write",
Addr: &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: 2135,
},
Err: grpcStatus.Error(grpcCodes.Unavailable, ""),
}),
brief: "network/write[0.0.0.0:2135](transport/Unavailable)",
},
{
err: grpcStatus.Error(grpcCodes.Unavailable, ""),
brief: "transport/Unavailable",
},
{
err: xerrors.Transport(
grpcStatus.Error(grpcCodes.Unavailable, ""),
),
brief: "transport/Unavailable",
},
{
err: xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
),
brief: "operation/BAD_REQUEST",
},
// errors with stack trace
{
err: xerrors.WithStackTrace(fmt.Errorf("test")),
brief: "unknown",
},
{
err: xerrors.WithStackTrace(io.EOF),
brief: "io/EOF",
},
{
err: xerrors.WithStackTrace(
grpcStatus.Error(grpcCodes.Unavailable, ""),
),
brief: "transport/Unavailable",
},
{
err: xerrors.WithStackTrace(xerrors.Transport(
grpcStatus.Error(grpcCodes.Unavailable, ""),
)),
brief: "transport/Unavailable",
},
{
err: xerrors.WithStackTrace(xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
)),
brief: "operation/BAD_REQUEST",
},
// joined errors
{
err: xerrors.Join(fmt.Errorf("test")),
brief: "unknown",
},
{
err: xerrors.Join(
fmt.Errorf("test"),
xerrors.Retryable(fmt.Errorf("test")),
),
brief: "retryable/CUSTOM",
},
{
err: xerrors.Join(
fmt.Errorf("test"),
xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
),
brief: "retryable/SomeName",
},
{
err: xerrors.Join(
fmt.Errorf("test"),
xerrors.Retryable(fmt.Errorf("test"), xerrors.WithName("SomeName")),
grpcStatus.Error(grpcCodes.Unavailable, "test"),
),
brief: "transport/Unavailable",
},
{
err: xerrors.Join(io.EOF),
brief: "io/EOF",
},
{
err: xerrors.Join(
grpcStatus.Error(grpcCodes.Unavailable, ""),
),
brief: "transport/Unavailable",
},
{
err: xerrors.Join(xerrors.Transport(
grpcStatus.Error(grpcCodes.Unavailable, ""),
)),
brief: "transport/Unavailable",
},
{
err: xerrors.Join(xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
)),
brief: "operation/BAD_REQUEST",
},
// joined errors with stack trace
{
err: xerrors.Join(xerrors.WithStackTrace(fmt.Errorf("test"))),
brief: "unknown",
},
{
err: xerrors.Join(xerrors.WithStackTrace(io.EOF)),
brief: "io/EOF",
},
{
err: xerrors.Join(xerrors.WithStackTrace(xerrors.Transport(
grpcStatus.Error(grpcCodes.Unavailable, ""),
))),
brief: "transport/Unavailable",
},
{
err: xerrors.Join(xerrors.WithStackTrace(xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
))),
brief: "operation/BAD_REQUEST",
},
// joined errors (mixed types)
{
err: xerrors.Join(
xerrors.WithStackTrace(fmt.Errorf("test")),
xerrors.WithStackTrace(io.EOF),
),
brief: "io/EOF",
},
{
err: xerrors.WithStackTrace(xerrors.Join(
xerrors.WithStackTrace(fmt.Errorf("test")),
xerrors.WithStackTrace(io.EOF),
)),
brief: "io/EOF",
},
{
err: xerrors.Join(
io.EOF,
grpcStatus.Error(grpcCodes.Unavailable, ""),
xerrors.WithStackTrace(xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
)),
),
brief: "io/EOF",
},
{
err: xerrors.Join(
&net.OpError{
Op: "write",
Addr: &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: 2135,
},
Err: grpcStatus.Error(grpcCodes.Unavailable, ""),
},
io.EOF,
grpcStatus.Error(grpcCodes.Unavailable, ""),
xerrors.WithStackTrace(xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
)),
),
brief: "io/EOF",
},
{
err: xerrors.Join(
grpcStatus.Error(grpcCodes.Unavailable, ""),
xerrors.WithStackTrace(xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
)),
),
brief: "transport/Unavailable",
},
{
err: xerrors.WithStackTrace(xerrors.Join(
xerrors.WithStackTrace(xerrors.Transport(
grpcStatus.Error(grpcCodes.Unavailable, ""),
)),
xerrors.WithStackTrace(xerrors.Operation(
xerrors.WithStatusCode(Ydb.StatusIds_BAD_REQUEST),
)),
)),
brief: "transport/Unavailable",
},
} {
t.Run("", func(t *testing.T) {
require.Equal(t, tt.brief, errorBrief(tt.err))
})
}
}

0 comments on commit bd8617f

Please sign in to comment.