-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr.go
121 lines (108 loc) · 2.27 KB
/
err.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package logex
import (
"bytes"
"errors"
"fmt"
"path"
"runtime"
"strconv"
"strings"
)
func NewError(info string, format ...interface{}) *TrackError {
err := errors.New(fmt.Sprintf(info, format...))
return &TrackError{
error: err,
}
}
func Is(e1, e2 error) bool {
if e, ok := e1.(*TrackError); ok {
e1 = e.error
}
if e, ok := e2.(*TrackError); ok {
e2 = e.error
}
return e1 == e2
}
type TrackError struct {
error
format []interface{}
stack []string
}
func (t *TrackError) Error() string {
if t == nil {
return "<NILTrackErr>"
}
if t.format == nil {
if t.error == nil {
panic(t.stack)
}
return t.error.Error()
}
return fmt.Sprintf(t.error.Error(), t.format...)
}
func (t *TrackError) Follow(err error) *TrackError {
if t == nil {
return nil
}
if te, ok := err.(*TrackError); ok {
if len(te.stack) > 0 {
te.stack[len(te.stack)-1] += ":" + err.Error()
}
t.stack = append(te.stack, t.stack...)
}
return t
}
func (t *TrackError) Format(obj ...interface{}) *TrackError {
if t == nil {
return nil
}
t.format = obj
return t
}
func (t *TrackError) StackError() string {
if t == nil {
return t.Error()
}
if len(t.stack) == 0 {
return t.Error()
}
return fmt.Sprintf("[%s] %s", strings.Join(t.stack, ";"), t.Error())
}
func Trackf(err error, obj ...interface{}) *TrackError {
e := TrackEx(1, err).Format(obj...)
return e
}
// set runtime info to error
func Track(err error, info ...interface{}) *TrackError {
return TrackEx(1, err, info...)
}
func joinInterface(info []interface{}, ch string) string {
ret := bytes.NewBuffer(make([]byte, 0, 512))
for idx, o := range info {
if idx > 0 {
ret.WriteString(ch)
}
ret.WriteString(fmt.Sprint(o))
}
return ret.String()
}
func TrackEx(depth int, err error, info ...interface{}) *TrackError {
if err == nil {
return nil
}
pc, _, line, _ := runtime.Caller(1 + depth)
name := runtime.FuncForPC(pc).Name()
name = path.Base(name)
stack := name + ":" + strconv.Itoa(line)
if len(info) > 0 {
stack += "(" + joinInterface(info, ",") + ")"
}
if te, ok := err.(*TrackError); ok {
te.stack = append(te.stack, stack)
return te
}
return &TrackError{err, nil, []string{stack}}
}
func NewTrackError(info ...interface{}) *TrackError {
return TrackEx(1, errors.New(fmt.Sprint(info...)))
}