-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
82 lines (67 loc) · 1.57 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
package skbtrace
import "fmt"
// Represents skbtrace instance which originated error
// Useful for tracking chain of errors
type ErrorLevel string
// Helper for prettier unwrapping errors
type Error interface {
Level() ErrorLevel
Ident() string
NextError() error
Message() string
}
const (
ErrMsgNotFound = "not found"
ErrMsgParseError = "parse error"
ErrMsgNotSpecified = "instance name is not specified"
)
const (
ErrLevelProbe ErrorLevel = "probe"
ErrLevelRow ErrorLevel = "row"
ErrLevelObject ErrorLevel = "object"
ErrLevelFilter ErrorLevel = "filter"
ErrLevelField ErrorLevel = "field"
ErrLevelStructDef ErrorLevel = "structdef"
)
type errorImpl struct {
level ErrorLevel
ident string
err error
msg string
}
func newErrorf(
level ErrorLevel, ident string, err error,
fmtstr string, args ...interface{},
) *errorImpl {
return &errorImpl{
level: level,
ident: ident,
err: err,
msg: fmt.Sprintf(fmtstr, args...),
}
}
func newCommonError(level ErrorLevel, ident string, msg string) *errorImpl {
return newErrorf(level, ident, nil, msg)
}
func (ewm *errorImpl) Level() ErrorLevel {
return ewm.level
}
func (ewm *errorImpl) Ident() string {
return ewm.ident
}
func (ewm *errorImpl) NextError() error {
return ewm.err
}
func (ewm *errorImpl) Message() string {
return ewm.msg
}
func (ewm *errorImpl) Error() string {
msg := fmt.Sprintf("error in %s '%s'", ewm.level, ewm.ident)
if ewm.msg != "" {
msg = fmt.Sprintf("%s: %s", msg, ewm.msg)
}
if ewm.err != nil {
msg = fmt.Sprintf("%s: %v", msg, ewm.err)
}
return msg
}