-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
95 lines (83 loc) · 1.95 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
83
84
85
86
87
88
89
90
91
92
93
94
95
package xin
import (
"errors"
"fmt"
"runtime"
)
//WrapError WrapError
type WrapError interface {
error
//Wrap Wrap a error in current error
Wrap(error)
}
//TracedError a WrapError store the position where error occur
type TracedError struct {
File string
Line int
Err error
}
//Error error interface
func (e *TracedError) Error() string {
return fmt.Sprintf("%s (%s:%d)", e.Err, e.File, e.Line)
}
//Unwrap use for errors.Unwrap
func (e *TracedError) Unwrap() error {
return e.Err
}
//Wrap WrapError interface,do not call this function directly , use WrapE func instead
func (e *TracedError) Wrap(err error) {
e.Err = err
_, e.File, e.Line, _ = runtime.Caller(2)
}
//WrapE wrap the given into the given WrapError
func WrapE(Err WrapError, err error) error {
Err.Wrap(Err)
return Err
}
//WrapEf create an error by format string and wrap it into the given WrapError
func WrapEf(Err WrapError, format string, a ...interface{}) error {
var wErr error
if len(a) > 0 {
wErr = fmt.Errorf(format, a...)
} else {
wErr = errors.New(format)
}
Err.Wrap(wErr)
return Err
}
//NewTracedE create a new TracedError and wrap the given error into it
func NewTracedE(err error) error {
if err == nil {
return nil
}
e := &TracedError{}
e.Wrap(err)
return e
}
//NewWrapEf @deprecated use NewTracedEf instead
// create an error by format string and wrap it into a new TracedError
func NewWrapEf(format string, a ...interface{}) error {
e := &TracedError{}
var wErr error
if len(a) > 0 {
wErr = fmt.Errorf(format, a...)
} else {
wErr = errors.New(format)
}
e.Wrap(wErr)
return e
}
//NewTracedEf create an error by format string and wrap it into a new TracedError
func NewTracedEf(format string, a ...interface{}) error {
e := &TracedError{}
var wErr error
if len(a) > 0 {
wErr = fmt.Errorf(format, a...)
} else {
wErr = errors.New(format)
}
e.Wrap(wErr)
return e
}
//InternalError an error for framework internal use
type InternalError struct{ TracedError }