forked from geofffranks/spruce
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
97 lines (80 loc) · 2.37 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package spruce
import (
"fmt"
"sort"
"strings"
"github.com/geofffranks/spruce/log"
"github.com/starkandwayne/goutils/ansi"
)
// MultiError ...
type MultiError struct {
Errors []error
}
// Error ...
func (e MultiError) Error() string {
s := []string{}
for _, err := range e.Errors {
s = append(s, fmt.Sprintf(" - %s\n", err))
}
sort.Strings(s)
return ansi.Sprintf("@r{%d} error(s) detected:\n%s\n", len(e.Errors), strings.Join(s, ""))
}
// Count ...
func (e *MultiError) Count() int {
return len(e.Errors)
}
// Append ...
func (e *MultiError) Append(err error) {
if err == nil {
return
}
if mult, ok := err.(MultiError); ok {
e.Errors = append(e.Errors, mult.Errors...)
} else {
e.Errors = append(e.Errors, err)
}
}
//WarningError should produce a warning message to stderr if the context set for
// the error fits the context the error was caught in.
type WarningError struct {
warning string
context ErrorContext
}
//An ErrorContext is a flag or set of flags representing the contexts that
// an error should have a special meaning in.
type ErrorContext uint
//Bitwise-or these together to represent several contexts
const (
eContextAll = 0
eContextDefaultMerge = 1 << iota
)
var dontPrintWarning bool
//NewWarningError returns a new WarningError object that has the given warning
// message and context(s) assigned. Assigning no context should mean that all
// contexts are active. Ansi library enabled.
func NewWarningError(context ErrorContext, warning string, args ...interface{}) (err WarningError) {
err.warning = ansi.Sprintf(warning, args...)
err.context = context
return
}
//SilenceWarnings when called with true will make it so that warnings will not
// print when Warn is called. Calling it with false will make warnings visible
// again. Warnings will print by default.
func SilenceWarnings(should bool) {
dontPrintWarning = should
}
//Error will return the configured warning message as a string
func (e WarningError) Error() string {
return e.warning
}
//HasContext returns true if the WarningError was configured with the given context (or all).
// False otherwise.
func (e WarningError) HasContext(context ErrorContext) bool {
return e.context == 0 || (context&e.context > 0)
}
//Warn prints the configured warning to stderr.
func (e WarningError) Warn() {
if !dontPrintWarning {
log.PrintfStdErr(ansi.Sprintf("@Y{warning:} %s\n", e.warning))
}
}