-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
190 lines (172 loc) · 4.47 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
188
189
190
package gors
import (
"errors"
"fmt"
"github.com/go-leo/gors/internal/pkg/cause"
"github.com/go-leo/gors/internal/pkg/status"
"github.com/go-leo/gox/convx"
"github.com/go-leo/gox/errorx"
"google.golang.org/grpc/codes"
gstatus "google.golang.org/grpc/status"
"io"
"regexp"
)
// 全局默认错误码,可以自定义覆盖
var (
UnknownStatusCode = 500
UnknownCode = 100001
UnknownMessage = "An internal server error occurred"
)
var msgRegExp = regexp.MustCompile("^gors.Error, StatusCode: (\\d+), Code: (\\d+), Message: (.+)$")
// Status 代表业务状态信息.
type Status struct {
Code int `json:"code,omitempty" yaml:"code,omitempty" xml:"code,omitempty" toml:"code,omitempty" codec:"code,omitempty" mapstructure:"code,omitempty"`
Message string `json:"message,omitempty" yaml:"message,omitempty" xml:"message,omitempty" toml:"message,omitempty" codec:"message,omitempty" mapstructure:"message,omitempty"`
}
type ErrorAPI interface {
Error() string
Wrap(err error) Error
Unwrap() error
Status() *Status
Is(err error) bool
GRPCStatus() *gstatus.Status
}
// Error 包含业务状态的错误.
type Error struct {
// StatusCode http status code
StatusCode int
// Code business service code
Code int
// Message error message
Message string
// Cause error
Cause error
}
// Froze convert Error to ErrorAPI
func (e Error) Froze() ErrorAPI {
return Error{
StatusCode: e.StatusCode,
Code: e.Code,
Message: e.Message,
Cause: e.Cause,
}
}
func (e Error) Wrap(err error) Error {
if e.Cause == nil {
return Error{
StatusCode: e.StatusCode,
Code: e.Code,
Message: e.Message,
Cause: err,
}
}
return Error{
StatusCode: e.StatusCode,
Code: e.Code,
Message: e.Message,
Cause: errorx.Join(e.Cause, err),
}
}
func (e Error) Error() string {
return fmt.Sprintf("gors.Error, StatusCode: %d, Code: %d, Message: %s", e.StatusCode, e.Code, e.Message)
}
func (e Error) Status() *Status {
return &Status{Code: e.Code, Message: e.Message}
}
// Unwrap provides compatibility for Go 1.13 error chains.
func (e Error) Unwrap() error { return e.Cause }
// Is matches each error in the chain with the target value.
func (e Error) Is(err error) bool {
ge := FromError(err)
return ge.StatusCode == e.StatusCode && ge.Code == e.Code
}
// GRPCStatus returns the Status represented by gors.Error.
func (e Error) GRPCStatus() *gstatus.Status {
// 如果StatusCode=200,转成 grpc的code是ok,WithDetails就会报错,导致s是nil
gs := gstatus.New(codes.Internal, e.Error())
if e.Cause != nil {
gs, _ = gs.WithDetails(&cause.Error{Msg: e.Cause.Error()})
}
return gs
}
// Format nolint: errcheck // WriteString could no check in pkg.
func (e Error) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%s\n", e.Error())
if e.Cause != nil {
fmt.Fprintf(s, "%+v", e.Cause)
}
return
}
if s.Flag('-') {
fmt.Fprintf(s, "%s\n", e.Error())
if e.Cause != nil {
fmt.Fprintf(s, "%-v", e.Cause)
}
return
}
fallthrough
case 's':
_, _ = io.WriteString(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.Error())
}
}
// FromError 解析error为 Error.
// 解析 Error 或者 status.Status 为 Error, 其他类型的error返回 UnknownError.
func FromError(err error) Error {
if err == nil {
return Error{}
}
if ret := errValue(); errors.As(err, &ret) {
return ret
}
if ret := new(Error); errors.As(err, &ret) {
return *ret
}
if ge, ok := gstatus.FromError(err); ok {
ret := Error{
StatusCode: status.FromGRPCCode(ge.Code()),
Code: UnknownCode,
Message: ge.Message(),
Cause: nil,
}
if e, ok := ErrorFromMessage(ge.Message()); ok {
ret = e
}
for _, detail := range ge.Details() {
if d, ok := detail.(*cause.Error); ok {
ret.Cause = errors.New(d.Msg)
break
}
}
return ret
}
if ret, ok := ErrorFromMessage(err.Error()); ok {
return ret
}
return Error{
StatusCode: UnknownStatusCode,
Code: UnknownCode,
Message: UnknownMessage,
Cause: err,
}
}
func ErrorFromMessage(msg string) (Error, bool) {
if !msgRegExp.MatchString(msg) {
return Error{}, false
}
subStrings := msgRegExp.FindAllStringSubmatch(msg, -1)
if len(subStrings) != 1 {
return Error{}, false
}
if len(subStrings[0]) != 4 {
return Error{}, false
}
return Error{StatusCode: convx.ToInt(subStrings[0][1]), Code: convx.ToInt(subStrings[0][2]), Message: subStrings[0][3]}, true
}
func errValue() Error {
return Error{}
}