Replaced Errors interface with concrete type Errors.

This commit is contained in:
Sander Schobers 2021-08-13 10:11:13 +02:00
parent fd774b775e
commit ffc7c1e89b
4 changed files with 23 additions and 29 deletions

View File

@ -35,12 +35,11 @@ function Open(p string) (*File, error) {
**Errors** **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 { type Errors struct {
error Errs []error
Errs() []error
} }
``` ```

View File

@ -66,8 +66,8 @@ func TestDeopterMultipleErrors(t *testing.T) {
var err = d.Invoke() var err = d.Invoke()
assert.NotNil(t, err) assert.NotNil(t, err)
require.Implements(t, (*Errors)(nil), err) require.IsType(t, &Errors{}, err)
var errs = err.(Errors).Errs() var errs = err.(*Errors).Errs
require.Equal(t, 2, len(errs)) require.Equal(t, 2, len(errs))
assert.True(t, o1.exec) assert.True(t, o1.exec)
assert.True(t, o2.exec) assert.True(t, o2.exec)

View File

@ -5,24 +5,17 @@ import (
"fmt" "fmt"
) )
var _ Errors = errorAggregate{} var _ error = &Errors{}
// Errors exposes slice of errors. // Errors exposes slice of errors.
type Errors interface { type Errors struct {
error Errs []error
Errs() []error
} }
type errorAggregate []error func (e Errors) Error() string {
func (e errorAggregate) Errs() []error {
return e
}
func (e errorAggregate) Error() string {
var msg = &bytes.Buffer{} var msg = &bytes.Buffer{}
fmt.Fprint(msg, "errors: ") fmt.Fprint(msg, "errors: ")
for i, err := range e { for i, err := range e.Errs {
if 0 < i { if 0 < i {
fmt.Fprint(msg, "; ") 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. // 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 { func ErrCombine(errs ...error) error {
if 0 < len(errs) { if len(errs) > 0 {
if e, ok := errs[0].(Errors); ok { if e, ok := errs[0].(*Errors); ok {
errs = append(e.Errs(), errs[1:]...) errs = append(e.Errs, errs[1:]...)
} }
} }
for i := 0; i < len(errs); i++ { for i := 0; i < len(errs); i++ {
@ -50,5 +43,7 @@ func ErrCombine(errs ...error) error {
} else if 1 == len(errs) { } else if 1 == len(errs) {
return errs[0] return errs[0]
} }
return errorAggregate(errs) return &Errors{
Errs: errs,
}
} }

View File

@ -35,8 +35,8 @@ func TestErrCombineAggregates(t *testing.T) {
var err = ErrCombine(err1, err2) var err = ErrCombine(err1, err2)
assert.NotNil(t, err) assert.NotNil(t, err)
require.Implements(t, (*Errors)(nil), err) require.IsType(t, &Errors{}, err)
var errs = err.(Errors).Errs() var errs = err.(*Errors).Errs
assert.Equal(t, 2, len(errs)) assert.Equal(t, 2, len(errs))
assert.Equal(t, err1, errs[0]) assert.Equal(t, err1, errs[0])
assert.Equal(t, err2, errs[1]) assert.Equal(t, err2, errs[1])
@ -60,8 +60,8 @@ func TestErrCombineExpandsFirstArgument(t *testing.T) {
var err = ErrCombine(ErrCombine(err1, err2), err3) var err = ErrCombine(ErrCombine(err1, err2), err3)
assert.NotNil(t, err) assert.NotNil(t, err)
require.Implements(t, (*Errors)(nil), err) require.IsType(t, &Errors{}, err)
var errs = err.(Errors).Errs() var errs = err.(*Errors).Errs
assert.Equal(t, 3, len(errs)) assert.Equal(t, 3, len(errs))
assert.Equal(t, err1, errs[0]) assert.Equal(t, err1, errs[0])
assert.Equal(t, err2, errs[1]) assert.Equal(t, err2, errs[1])
@ -76,9 +76,9 @@ func TestErrCombineDoesNotExpandConsecutiveArguments(t *testing.T) {
var err = ErrCombine(err1, ErrCombine(err2, err3)) var err = ErrCombine(err1, ErrCombine(err2, err3))
assert.NotNil(t, err) assert.NotNil(t, err)
require.Implements(t, (*Errors)(nil), err) require.IsType(t, &Errors{}, err)
var errs = err.(Errors).Errs() var errs = err.(*Errors).Errs
assert.Equal(t, 2, len(errs)) assert.Equal(t, 2, len(errs))
assert.Equal(t, err1, errs[0]) assert.Equal(t, err1, errs[0])
assert.Implements(t, (*Errors)(nil), errs[1]) assert.IsType(t, &Errors{}, errs[1])
} }