Skip to content

Commit

Permalink
feat(errorx): batch error add API to expose the underlying errors lik…
Browse files Browse the repository at this point in the history
…e multierr
  • Loading branch information
zcong1993 committed Oct 16, 2023
1 parent 87b7a11 commit be071b6
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
31 changes: 31 additions & 0 deletions core/errorx/batcherror.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ type (
errorArray []error
)

// Errors returns a slice containing zero or more errors that the supplied
// error is composed of. If the error is nil, a nil slice is returned.
func Errors(err error) []error {
if err == nil {
return nil
}

if be, ok := err.(*BatchError); ok {
return be.Errors()
}

return []error{err}
}

// Add adds errs to be, nil errors are ignored.
func (be *BatchError) Add(errs ...error) {
for _, err := range errs {
Expand All @@ -37,6 +51,23 @@ func (be *BatchError) NotNil() bool {
return len(be.errs) > 0
}

// Errors returns the list of underlying errors.
// Callers of this function are free to modify the returned slice.
func (be *BatchError) Errors() []error {
if be == nil {
return nil
}
return append([]error(nil), be.errs...)
}

// Error implements error interface. This allows users to convert error to BatchError.
func (be *BatchError) Error() string {
if be == nil || be.Err() == nil {
return ""
}
return be.Err().Error()
}

// Error returns a string that represents inside errors.
func (ea errorArray) Error() string {
var buf bytes.Buffer
Expand Down
95 changes: 95 additions & 0 deletions core/errorx/batcherror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,98 @@ func TestBatchErrorWithErrors(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%s\n%s", err1, err2), batch.Err().Error())
assert.True(t, batch.NotNil())
}

func TestBatchError_Error(t *testing.T) {
type fields struct {
be *BatchError
}
tests := []struct {
name string
fields fields
want string
}{
{
"nil",
fields{nil},
"",
},
{
"nil errors",
fields{&BatchError{}},
"",
},
{
"one error",
fields{&BatchError{errs: errorArray{errors.New(err1)}}},
err1,
},
{
"two errors",
fields{&BatchError{errs: errorArray{errors.New(err1), errors.New(err2)}}},
fmt.Sprintf("%s\n%s", err1, err2),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
be := tt.fields.be
assert.Equalf(t, tt.want, be.Error(), "Error()")
})
}
}

func TestErrors(t *testing.T) {
e1 := errors.New(err1)
e2 := errors.New(err2)

type args struct {
err error
}
tests := []struct {
name string
args args
want []error
}{
{
"nil",
args{nil},
nil,
},
{
"nil errors",
args{&BatchError{}},
nil,
},
{
"one error",
args{e1},
[]error{e1},
},
{
"BatchError - one error",
args{&BatchError{errs: errorArray{e1}}},
[]error{e1},
},
{
"BatchError - two errors",
args{&BatchError{errs: errorArray{e1, e2}}},
[]error{e1, e2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, Errors(tt.args.err), "Errors(%v)", tt.args.err)
})
}
}

func TestBatchError_Errors(t *testing.T) {
var be *BatchError
assert.Nil(t, be.Errors())

e1 := errors.New(err1)
e2 := errors.New(err2)

be = &BatchError{}
be.Add(e1, e2)
assert.Equal(t, []error{e1, e2}, be.Errors())
}

0 comments on commit be071b6

Please sign in to comment.