diff --git a/pkg/atomix/errors/errors.go b/pkg/atomix/errors/errors.go index 26499ac..0bfd9a4 100644 --- a/pkg/atomix/errors/errors.go +++ b/pkg/atomix/errors/errors.go @@ -17,6 +17,7 @@ package errors import ( "context" "fmt" + "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) @@ -51,6 +52,8 @@ const ( Internal // Fault indicates a data fault occurred Fault + // Aborted indicates a request is aborted + Aborted ) // TypedError is an typed error @@ -120,6 +123,9 @@ func From(err error) error { return NewInternal(status.Message()) case codes.DataLoss: return NewFault(status.Message()) + case codes.Aborted: + return NewAborted(status.Message()) + default: return err } @@ -167,6 +173,8 @@ func Proto(err error) error { return status.Error(codes.Internal, typed.Message) case Fault: return status.Error(codes.DataLoss, typed.Message) + case Aborted: + return status.Error(codes.Aborted, typed.Message) default: return status.Error(codes.Internal, err.Error()) } @@ -248,6 +256,11 @@ func NewFault(msg string, args ...interface{}) error { return New(Fault, msg, args...) } +// NewAborted returns a new Aborted error +func NewAborted(msg string, args ...interface{}) error { + return New(Aborted, msg, args...) +} + // Code returns the error code func Code(err error) int { return int(TypeOf(err)) @@ -333,3 +346,8 @@ func IsInternal(err error) bool { func IsFault(err error) bool { return IsType(err, Fault) } + +// IsAborted checkes whether the given error is an Aborted error +func IsAborted(err error) bool { + return IsType(err, Aborted) +} diff --git a/pkg/atomix/errors/errors_test.go b/pkg/atomix/errors/errors_test.go index 4a32e0b..4bda2be 100644 --- a/pkg/atomix/errors/errors_test.go +++ b/pkg/atomix/errors/errors_test.go @@ -16,8 +16,9 @@ package errors import ( "errors" - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestFactories(t *testing.T) { @@ -45,6 +46,8 @@ func TestFactories(t *testing.T) { assert.Equal(t, "Timeout", NewTimeout("Timeout").Error()) assert.Equal(t, Internal, NewInternal("").(*TypedError).Type) assert.Equal(t, "Internal", NewInternal("Internal").Error()) + assert.Equal(t, Aborted, NewAborted("").(*TypedError).Type) + assert.Equal(t, "Aborted", NewAborted("Aborted").Error()) } func TestPredicates(t *testing.T) { @@ -72,4 +75,6 @@ func TestPredicates(t *testing.T) { assert.True(t, IsTimeout(NewTimeout("Timeout"))) assert.False(t, IsInternal(errors.New("Internal"))) assert.True(t, IsInternal(NewInternal("Internal"))) + assert.False(t, IsAborted(errors.New("Aborted"))) + assert.True(t, IsAborted(NewAborted("Aborted"))) }