-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
187 lines (156 loc) · 3.73 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package errgo
import (
"encoding/json"
"fmt"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"google.golang.org/grpc/codes"
)
// Error is a custom error type that contains the error message, file, function, line, code, and stack trace.
type Error struct {
Err error
Message string
File string
Function string
Line int
Code codes.Code
StackTrace string
}
// MarshalJSON implements the json.Marshaler interface.
func (e Error) MarshalJSON() ([]byte, error) {
var errMsg []byte
var err error
switch e.Err.(type) {
case *Error:
errMsg, err = json.Marshal(e.Err)
default:
errMsg, err = json.Marshal(e.Err.Error())
}
if err != nil {
return nil, err
}
var errInterface interface{}
if err := json.Unmarshal(errMsg, &errInterface); err != nil {
return nil, err
}
return json.Marshal(map[string]interface{}{
"error": errInterface,
"message": e.Message,
"file": e.File,
"function": e.Function,
"line": e.Line,
"code": fmt.Sprintf("%s (%d)", e.Code.String(), e.Code),
"stack_trace": e.StackTrace,
})
}
// Error implements the error interface.
func (e *Error) Error() string {
return fmt.Sprintf("%s (%d): %s", e.Code.String(), e.Code, e.Message)
}
func getStackInfo(stackDepth int) (string, string, int) {
result_file := "?"
result_func := "?()"
result_line := 0
if pc, file, line, ok := runtime.Caller(stackDepth + 1); ok {
result_file = filepath.Base(file)
result_line = line
if fn := runtime.FuncForPC(pc); fn != nil {
_, dotName, _ := strings.Cut(filepath.Base(fn.Name()), ".")
result_func = strings.TrimLeft(dotName, ".") + "()"
}
}
return result_file, result_func, result_line
}
// Unwrap returns the underlying error.
func Unwrap(err error) *Error {
if err == nil {
return nil
}
if e, ok := err.(*Error); ok {
return e
}
return nil
}
// GetRoot returns the root error.
func GetRoot(err error) *Error {
if err == nil {
return nil
}
var root *Error
for e := Unwrap(err); e != nil; e = Unwrap(e.Err) {
root = e
}
return root
}
// ContainsError returns true if the error contains the given error.
func ContainsError(left error, right error) bool {
if left == nil || right == nil {
return false
}
if left.Error() == right.Error() {
return true
}
for e := Unwrap(left); e != nil; e = Unwrap(e.Err) {
if e.Error() == right.Error() || e.Err.Error() == right.Error() {
return true
}
}
return false
}
// ContainsCode returns true if the error contains the given code.
func ContainsCode(left error, right codes.Code) bool {
if left == nil {
return false
}
if strings.Contains(left.Error(), right.String()) {
return true
}
for e := Unwrap(left); e != nil; e = Unwrap(e.Err) {
if e.Code == right || strings.Contains(e.Err.Error(), right.String()) {
return true
}
}
return false
}
// UnwrapAll returns all the errors in the error chain.
func UnwrapAll(err error) []*Error {
var result []*Error
if err == nil {
return result
}
for e := Unwrap(err); e != nil; e = Unwrap(e.Err) {
result = append(result, e)
}
return result
}
// JSON returns the error as a JSON string.
func (e *Error) JSON() string {
jsonBytes, err := json.MarshalIndent(e, "", " ")
if err != nil {
return ""
}
return string(jsonBytes)
}
// JSON returns the error as a JSON string.
func JSON(e error) string {
err := Unwrap(e)
if err != nil {
return err.JSON()
}
return e.Error()
}
// Wrap wraps the given error with the given message and code.
func Wrap(err error, msg string, code codes.Code) *Error {
file, function, line := getStackInfo(1)
return &Error{
Err: err,
Message: msg,
File: file,
Function: function,
Line: line,
Code: code,
StackTrace: string(debug.Stack()),
}
}