Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aborted error to errors package #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/atomix/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package errors
import (
"context"
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
}
7 changes: 6 additions & 1 deletion pkg/atomix/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ package errors

import (
"errors"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFactories(t *testing.T) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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")))
}