-
Notifications
You must be signed in to change notification settings - Fork 0
/
errutil.go
66 lines (55 loc) · 1.76 KB
/
errutil.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Package errutil provides common error types and utilities.
package errutil
import (
"fmt"
"io"
"golang.org/x/xerrors"
)
// ErrNotImplemented is returned when feature is currently not implemented.
var ErrNotImplemented = xerrors.New("not implemented")
// AssertionFailure is used for an error resulting from the failure of an
// expected invariant.
func AssertionFailure(format string, args ...interface{}) error {
return fmt.Errorf("assertion failure: "+format, args...)
}
// UnexpectedType builds an error for an unexpected type, typically in a type switch.
func UnexpectedType(t interface{}) error {
return AssertionFailure("unexpected type %T", t)
}
// UnhandledCase builds an error for an unexpected value in a switch.
func UnhandledCase(v interface{}) error {
return AssertionFailure("unhandled case %v", v)
}
// Errors is a collection of errors.
type Errors []error
// Add appends errors to the list.
func (e *Errors) Add(err ...error) {
*e = append(*e, err...)
}
// Err returns an error equivalent to this error list.
// If the list is empty, Err returns nil.
func (e Errors) Err() error {
if len(e) == 0 {
return nil
}
return e
}
// Error implements the error interface.
func (e Errors) Error() string {
switch len(e) {
case 0:
return "no errors"
case 1:
return e[0].Error()
}
return fmt.Sprintf("%s (and %d more errors)", e[0], len(e)-1)
}
// CheckClose closes c. If an error occurs it will be written to the error
// pointer errp, if it doesn't already reference an error. This is intended to
// allow you to properly check errors when defering a close call. In this case
// the error pointer should be the address of a named error return.
func CheckClose(errp *error, c io.Closer) {
if err := c.Close(); err != nil && *errp == nil {
*errp = err
}
}