-
Notifications
You must be signed in to change notification settings - Fork 669
/
errors.go
52 lines (42 loc) · 1.02 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package wrappers
import (
"strings"
)
var _ error = (*aggregate)(nil)
type Errs struct{ Err error }
func (errs *Errs) Errored() bool {
return errs.Err != nil
}
func (errs *Errs) Add(errors ...error) {
if errs.Err == nil {
for _, err := range errors {
if err != nil {
errs.Err = err
break
}
}
}
}
// NewAggregate returns an aggregate error from a list of errors
func NewAggregate(errs []error) error {
err := &aggregate{errs}
if len(err.Errors()) == 0 {
return nil
}
return err
}
type aggregate struct{ errs []error }
// Error returns the slice of errors with comma separated messsages wrapped in brackets
// [ error string 0 ], [ error string 1 ] ...
func (a *aggregate) Error() string {
errString := make([]string, len(a.errs))
for i, err := range a.errs {
errString[i] = "[" + err.Error() + "]"
}
return strings.Join(errString, ",")
}
func (a *aggregate) Errors() []error {
return a.errs
}