-
Notifications
You must be signed in to change notification settings - Fork 84
/
error.go
55 lines (48 loc) · 1.42 KB
/
error.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package error
import (
"errors"
"strings"
multierror "github.com/hashicorp/go-multierror"
)
// MultiErrorFunc is a helper to convert the standard multierror output into
// something a little more friendly to consoles.
func MultiErrorFunc(err []error) string {
points := make([]string, len(err))
for i, err := range err {
points[i] = err.Error()
}
return strings.Join(points, ", ")
}
// FormattedMultiError wraps any non-nil multierrors with the multiErrorFunc.
// It is safe to call in cases where the err may or may not be nil and will
// overwrite the existing formatter.
func FormattedMultiError(err *multierror.Error) error {
if err != nil {
err.ErrorFormat = MultiErrorFunc
}
return err.ErrorOrNil()
}
// StatusCoder is an error with an extra StatusCode method.
// mainly, nomad api.UnexpectedResponseError implements this.
type StatusCoder interface {
Error() string
Unwrap() error
StatusCode() int
}
// APIErrIs attempts to coerce err into an UnexpectedResponseError to check
// its status code. Failing that, it will look for str in the error string.
// If code==0 it will be ignored, same for str==""
func APIErrIs(err error, code int, str string) bool {
if err == nil {
return false
}
if code > 0 {
var sc StatusCoder
if errors.As(err, &sc) {
return sc.StatusCode() == code
}
}
return str != "" && strings.Contains(err.Error(), str)
}