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**
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
}
```

View File

@ -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)

View File

@ -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,
}
}

View File

@ -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])
}