diff --git a/README.md b/README.md index 232f7dc..a36017c 100644 --- a/README.md +++ b/README.md @@ -35,12 +35,11 @@ function Open(p string) (*File, error) { **Errors** -An `Errors` interface that represents multiple errors (but still adheres to the `error` interface) +An `Errors` struct that represents multiple errors (but still adheres to the `error` interface) ``` -type Errors interface { - error - Errs() []error +type Errors struct { + Errs []error } ``` diff --git a/deopter_test.go b/deopter_test.go index 7d0db1d..81671de 100644 --- a/deopter_test.go +++ b/deopter_test.go @@ -66,8 +66,8 @@ func TestDeopterMultipleErrors(t *testing.T) { var err = d.Invoke() assert.NotNil(t, err) - require.Implements(t, (*Errors)(nil), err) - var errs = err.(Errors).Errs() + require.IsType(t, &Errors{}, err) + var errs = err.(*Errors).Errs require.Equal(t, 2, len(errs)) assert.True(t, o1.exec) assert.True(t, o2.exec) diff --git a/errors.go b/errors.go index a4d28dc..381d4c4 100644 --- a/errors.go +++ b/errors.go @@ -5,24 +5,17 @@ import ( "fmt" ) -var _ Errors = errorAggregate{} +var _ error = &Errors{} // Errors exposes slice of errors. -type Errors interface { - error - Errs() []error +type Errors struct { + Errs []error } -type errorAggregate []error - -func (e errorAggregate) Errs() []error { - return e -} - -func (e errorAggregate) Error() string { +func (e Errors) Error() string { var msg = &bytes.Buffer{} fmt.Fprint(msg, "errors: ") - for i, err := range e { + for i, err := range e.Errs { if 0 < i { fmt.Fprint(msg, "; ") } @@ -33,9 +26,9 @@ func (e errorAggregate) Error() string { // ErrCombine combines one or more errors, nil entries are omitted and nil is returned if all given errors are nil. The first argument is expanded if it satisfies the Errors interface. In case of aggregation of errors the return error will satisfy the Errors interface. func ErrCombine(errs ...error) error { - if 0 < len(errs) { - if e, ok := errs[0].(Errors); ok { - errs = append(e.Errs(), errs[1:]...) + if len(errs) > 0 { + if e, ok := errs[0].(*Errors); ok { + errs = append(e.Errs, errs[1:]...) } } for i := 0; i < len(errs); i++ { @@ -50,5 +43,7 @@ func ErrCombine(errs ...error) error { } else if 1 == len(errs) { return errs[0] } - return errorAggregate(errs) + return &Errors{ + Errs: errs, + } } diff --git a/errors_test.go b/errors_test.go index e7f3e1f..c21742b 100644 --- a/errors_test.go +++ b/errors_test.go @@ -35,8 +35,8 @@ func TestErrCombineAggregates(t *testing.T) { var err = ErrCombine(err1, err2) assert.NotNil(t, err) - require.Implements(t, (*Errors)(nil), err) - var errs = err.(Errors).Errs() + require.IsType(t, &Errors{}, err) + var errs = err.(*Errors).Errs assert.Equal(t, 2, len(errs)) assert.Equal(t, err1, errs[0]) assert.Equal(t, err2, errs[1]) @@ -60,8 +60,8 @@ func TestErrCombineExpandsFirstArgument(t *testing.T) { var err = ErrCombine(ErrCombine(err1, err2), err3) assert.NotNil(t, err) - require.Implements(t, (*Errors)(nil), err) - var errs = err.(Errors).Errs() + require.IsType(t, &Errors{}, err) + var errs = err.(*Errors).Errs assert.Equal(t, 3, len(errs)) assert.Equal(t, err1, errs[0]) assert.Equal(t, err2, errs[1]) @@ -76,9 +76,9 @@ func TestErrCombineDoesNotExpandConsecutiveArguments(t *testing.T) { var err = ErrCombine(err1, ErrCombine(err2, err3)) assert.NotNil(t, err) - require.Implements(t, (*Errors)(nil), err) - var errs = err.(Errors).Errs() + require.IsType(t, &Errors{}, err) + var errs = err.(*Errors).Errs assert.Equal(t, 2, len(errs)) assert.Equal(t, err1, errs[0]) - assert.Implements(t, (*Errors)(nil), errs[1]) + assert.IsType(t, &Errors{}, errs[1]) }