-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
apperr.go
75 lines (64 loc) · 1.27 KB
/
apperr.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
package apperr
import "github.com/pkg/errors"
type Error struct {
root error
err error
contextInfo map[string]string
}
func New(err string, root error) error {
return &Error{
root: root,
err: errors.New(err),
}
}
func (e *Error) Error() string {
return e.err.Error()
}
func (e *Error) Is(err error) bool {
if err == nil {
return false
}
return errors.Is(e.root, err)
}
func (e *Error) Unwrap() error {
return e.err
}
//func (e *Error) Cause(err error) bool {
// return e.root
//}
func AddContext(err error, field, message string) {
err = errors.Cause(err)
if err == nil {
return
}
if customErr, ok := err.(*Error); ok {
if customErr.contextInfo == nil {
customErr.contextInfo = make(map[string]string)
}
customErr.contextInfo[field] = message
}
}
func GetContext(err error) map[string]string {
err = errors.Cause(err)
if err == nil {
return nil
}
if customErr, ok := err.(*Error); ok {
return customErr.contextInfo
}
return nil
}
func SetContext(err error, contextInfo map[string]string) {
if customErr, ok := err.(*Error); ok {
customErr.contextInfo = contextInfo
}
}
func SetRoot(err, root error) {
err = errors.Cause(err)
if err == nil {
return
}
if customErr, ok := err.(*Error); ok {
customErr.root = root
}
}