-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
80 lines (64 loc) · 1.18 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
package tracer
import (
"encoding/json"
"strings"
)
type Error struct {
Anno string `json:"anno,omitempty"`
Code string `json:"code,omitempty"`
Desc string `json:"desc,omitempty"`
Docs string `json:"docs,omitempty"`
Kind string `json:"kind,omitempty"`
Stck []string `json:"stck,omitempty"`
Wrpd error `json:"-"`
}
func (e *Error) Copy() *Error {
c := &Error{
Anno: e.Anno,
Code: e.Code,
Desc: e.Desc,
Docs: e.Docs,
Kind: e.Kind,
Stck: e.Stck,
Wrpd: e.Wrpd,
}
return c
}
func (e *Error) Error() string {
if e.Kind == "" && e.Anno != "" {
return e.Anno
}
kind := toStringCase(e.Kind)
if e.Kind != "" && e.Anno == "" {
return kind
}
return strings.TrimSuffix(kind, " error") + ": " + e.Anno
}
func (e *Error) GoString() string {
return mustJson(e)
}
func (e *Error) Is(x error) bool {
return cause(e) == cause(x)
}
func (e *Error) Unwrap() error {
return e.Wrpd
}
func cause(err error) error {
if err == nil {
return nil
}
e, ok := err.(*Error)
if ok {
if e.Wrpd != nil {
return e.Wrpd
}
}
return err
}
func mustJson(v interface{}) string {
b, err := json.Marshal(v)
if err != nil {
panic(err.Error())
}
return string(b)
}