-
Notifications
You must be signed in to change notification settings - Fork 2k
/
warning.go
45 lines (35 loc) · 906 Bytes
/
warning.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package helper
import (
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
)
// MergeMultierrorWarnings takes warnings and merges them into a returnable
// string. This method is used to return API and CLI errors.
func MergeMultierrorWarnings(errs ...error) string {
if len(errs) == 0 {
return ""
}
var mErr multierror.Error
_ = multierror.Append(&mErr, errs...)
mErr.ErrorFormat = warningsFormatter
return mErr.Error()
}
// warningsFormatter is used to format warnings.
func warningsFormatter(es []error) string {
sb := strings.Builder{}
switch len(es) {
case 0:
return ""
case 1:
sb.WriteString("1 warning:\n")
default:
sb.WriteString(fmt.Sprintf("%d warnings:\n", len(es)))
}
for _, err := range es {
sb.WriteString(fmt.Sprintf("\n* %s", strings.TrimSpace(err.Error())))
}
return sb.String()
}