-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
102 lines (84 loc) · 2.16 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
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
98
99
100
101
102
package util
import (
"fmt"
"io"
"os"
"strings"
"github.com/pkg/errors"
)
type ErrorHandler struct {
Out io.Writer
PrintStackTrace bool
}
// The default error handler
var E = ErrorHandler{
Out: os.Stderr,
PrintStackTrace: true,
}
// New creates a new error variable
func (e *ErrorHandler) New(format string, a ...interface{}) error {
return errors.New(fmt.Sprintf(format, a...))
}
// Annotate increases context information to the error
func (e *ErrorHandler) Annotate(err error, a ...interface{}) error {
return errors.WithMessage(err, fmt.Sprint(a...))
}
// Print writes the error message to predefined io.Writer
func (e *ErrorHandler) Print(err error, a ...interface{}) {
fmt.Fprint(e.Out, "Error: ")
if len(a) > 0 {
fmt.Fprint(e.Out, a...)
fmt.Fprint(e.Out, ": ")
}
format := "%s\n"
if e.PrintStackTrace {
format = "%+v"
}
fmt.Fprintf(e.Out, format, err)
}
func (e *ErrorHandler) Panic(err error, a ...interface{}) {
e.Print(err, a...)
panic("Irrecoverable error")
}
// ErrorList is a list of errors that can be printed in a single go
type ErrorList struct {
Message string
errors []error
}
// NewErrorList returns an initialized ErrorList
func NewErrorList(message string) (ret *ErrorList) {
return &ErrorList{
Message: message,
}
}
// Append adds a new error to the list
func (e *ErrorList) Append(err error) {
e.errors = append(e.errors, err)
}
// Error returns the error string generated from the list
func (e *ErrorList) Error() string {
if len(e.errors) == 0 {
return ""
}
ret := "Error: " + e.Message + ": "
for i := range e.errors {
ret = ret + fmt.Sprintf("Error %d: %s; ", i+1, e.errors[i])
}
return ret
}
// IsEmpty returns true if the error list is empty
func (e *ErrorList) IsEmpty() bool {
return len(e.errors) == 0
}
// Fault checks if given err is != nil and if it is runs os.Exit(1). This
// should be used as a top level handling of errors in the main()
func Fault(err error, message string, arg ...string) {
if err != nil {
msg := ""
if err != nil {
msg = fmt.Sprintf(" (error: %s)", err)
}
fmt.Fprintf(os.Stderr, "Error: %s%s.%s\n", message, strings.Join(arg, " "), msg)
os.Exit(1)
}
}