-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
143 lines (124 loc) · 2.77 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package errors
import (
"fmt"
"io"
)
type Error struct {
msg string
errorType interface{}
originalError error
*stack
}
type causer interface {
Cause() error
}
func New(msg string) error {
return Error{msg: msg, errorType: "", originalError: nil, stack: callers()}
}
func Newf(format string, args ...interface{}) error {
msg := fmt.Sprintf(format, args...)
return Error{msg: msg, errorType: "", originalError: nil, stack: callers()}
}
func Errorf(format string, args ...interface{}) error {
return Error{
msg: fmt.Sprintf(format, args...),
errorType: "",
originalError: nil,
stack: callers(),
}
}
func Wrap(err error, msg string) error {
if err == nil {
return nil
}
if msg != "" {
msg += ": "
}
if customErr, ok := err.(Error); ok {
customErr.stack = nil
return Error{
msg: fmt.Sprintf("%s%s", msg, customErr.msg), errorType: customErr.errorType,
originalError: customErr,
stack: callers(),
}
}
return Error{msg: fmt.Sprintf("%s%s", msg, err.Error()), errorType: "", originalError: err, stack: callers()}
}
func Wrapf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
msg := fmt.Sprintf(format, args...)
if msg != "" {
msg += ": "
}
if customErr, ok := err.(Error); ok {
customErr.stack = nil
return Error{
msg: fmt.Sprintf("%s%s", msg, customErr.msg),
errorType: customErr.errorType,
originalError: customErr,
stack: callers(),
}
}
return Error{msg: fmt.Sprintf("%s%s", msg, err.Error()), errorType: "", originalError: err, stack: callers()}
}
func GetType(err error) interface{} {
if customErr, ok := err.(Error); ok {
return customErr.errorType
}
return ""
}
func SetType(err error, errorType interface{}) (error, bool) {
if customErr, ok := err.(Error); ok {
customErr.errorType = errorType
return customErr, true
}
return err, false
}
func SetTypeWithoutBool(err error, errorType interface{}) error {
err, _ = SetType(err, errorType)
return err
}
func Cause(err error) error {
var oldErr error
for err != nil {
oldErr = err
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
if err == nil {
err = error(oldErr)
break
}
}
return err
}
func (err Error) Cause() error {
return err.originalError
}
func (err Error) Error() string {
return err.msg
}
func (err Error) Unwrap() error {
return err.originalError
}
func (err Error) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
io.WriteString(s, err.Error())
fmt.Fprintf(s, "%+v", err.stack)
} else if s.Flag('#') {
fmt.Fprintf(s, "%+v", err.stack)
} else {
io.WriteString(s, err.Error())
}
case 's':
io.WriteString(s, err.Error())
case 'q':
fmt.Fprintf(s, "%q", err.Error())
}
}