-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
90 lines (78 loc) · 1.89 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
package glog
import (
"os"
"strings"
)
var stackTracer = NewStackTracer()
type GErrorTrace struct {
depth int
line int
file string
method string
}
type GError struct {
error
isFatalError bool
exitCode int
}
// Related config setting(s):
//
// - `LoggerConfig.ColorIndicatorDebug`
func (ge *GError) printStackTrace(maxLevel int, logger *Logger) {
stackTracer.Sample(maxLevel).PrintWithLogger(logger, 'x')
}
func (ge *GError) check(err error, msg string, logger *Logger) bool {
if err == nil {
return false
}
es := err.Error()
ges := ge.Error()
if err == ge.error || es == ges || strings.HasSuffix(es, ": "+ges) || strings.HasPrefix(es, ges+": ") || strings.Contains(es, ": "+ges+": ") {
logger.Error("%s: %s", msg, Error(err))
if ge.isFatalError {
ge.printStackTrace(0, logger)
logger.Error("%s (exit status %s: %s)", Bold()+WrapRed("FATAL"), Int(ge.exitCode), err.Error())
os.Exit(ge.exitCode)
} else {
ge.printStackTrace(1, logger)
return true
}
}
return false
}
func NewGError(err error, isFatal bool, exitCode int) *GError {
ge := &GError{
error: err,
isFatalError: isFatal,
exitCode: exitCode,
}
return ge
}
type GErrorRegistry struct {
registeredErrors []*GError
logger *Logger
}
func (ger *GErrorRegistry) Append(errors ...*GError) *GErrorRegistry {
ger.registeredErrors = append(ger.registeredErrors, errors...)
return ger
}
func (ger *GErrorRegistry) Register(err error, isFatal bool, exitCode int) *GErrorRegistry {
return ger.Append(NewGError(err, isFatal, exitCode))
}
func (ger *GErrorRegistry) Check(err error, msg string) {
if err == nil {
return
}
for _, ge := range ger.registeredErrors {
if ge.check(err, msg, ger.logger) {
break
}
}
}
func NewGErrorRegistry() *GErrorRegistry {
ger := &GErrorRegistry{
registeredErrors: []*GError{},
logger: NewLoggerSimple("Errors"),
}
return ger
}