-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.custom.go
143 lines (117 loc) · 3.04 KB
/
error.custom.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 errorcode
import (
errorsi "github.com/hopeio/cherry/utils/errors"
"github.com/hopeio/cherry/utils/log"
stringsi "github.com/hopeio/cherry/utils/strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"strconv"
)
func (x ErrCode) Code() int {
return int(x)
}
func (x ErrCode) ErrRep() *ErrRep {
return &ErrRep{Code: x, Message: x.String()}
}
func (x ErrCode) Rep() *ErrRep {
return &ErrRep{Code: x, Message: x.String()}
}
// example 实现
func (x ErrCode) GrpcStatus() *status.Status {
return status.New(codes.Code(x), x.String())
}
func (x ErrCode) Message(msg string) *ErrRep {
return &ErrRep{Code: x, Message: msg}
}
func (x ErrCode) Warp(err error) *ErrRep {
return &ErrRep{Code: x, Message: err.Error()}
}
func (x ErrCode) Error() string {
return x.String()
}
func ErrHandle(err interface{}) error {
if e, ok := err.(*ErrRep); ok {
return e
}
if e, ok := err.(ErrCode); ok {
return e.ErrRep()
}
if e, ok := err.(*status.Status); ok {
return e.Err()
}
if e, ok := err.(*errorsi.ErrRep); ok {
return e
}
if e, ok := err.(errorsi.ErrCode); ok {
return e.ErrRep()
}
if e, ok := err.(error); ok {
return Unknown.Message(e.Error())
}
return Unknown.ErrRep()
}
func Code(err error) int {
switch v := err.(type) {
case *ErrRep:
return int(v.Code)
case ErrCode:
return int(v)
case *errorsi.ErrRep:
return int(v.Code)
case errorsi.ErrCode:
return int(v)
}
return 0
}
func (x ErrCode) Origin() errorsi.ErrCode {
return errorsi.ErrCode(x)
}
func (x ErrCode) OriErrRep() *errorsi.ErrRep {
return &errorsi.ErrRep{Code: errorsi.ErrCode(x), Message: x.String()}
}
func (x ErrCode) OriMessage(msg string) *errorsi.ErrRep {
return &errorsi.ErrRep{Code: errorsi.ErrCode(x), Message: msg}
}
func (x ErrCode) OriWarp(err error) *errorsi.ErrRep {
return &errorsi.ErrRep{Code: errorsi.ErrCode(x), Message: err.Error()}
}
func (x ErrCode) OriLog(err error) *errorsi.ErrRep {
log.Error(err)
return &errorsi.ErrRep{Code: errorsi.ErrCode(x), Message: x.String()}
}
func (x *ErrRep) Error() string {
return x.Message
}
func (x *ErrRep) GrpcStatus() *status.Status {
return status.New(codes.Code(x.Code), x.Message)
}
func (x *ErrRep) MarshalJSON() ([]byte, error) {
return stringsi.ToBytes(`{"code":` + strconv.Itoa(int(x.Code)) + `,"message":"` + x.Message + `"}`), nil
}
/*func (x ErrCode) MarshalJSON() ([]byte, error) {
return stringsi.ToBytes(`{"code":` + strconv.Itoa(int(x)) + `,"message":"` + x.String() + `"}`), nil
}
*/
func FromError(err error) (s *ErrRep, ok bool) {
if err == nil {
return nil, true
}
if se, ok := err.(ErrRepInterface); ok {
return se.ErrRep(), true
}
return NewErrRep(codes.Unknown, err.Error()), false
}
func SuccessRep() *ErrRep {
return &ErrRep{Code: Success, Message: Success.Error()}
}
func NewErrRep[E ErrCodeGeneric](code E, msg string) *ErrRep {
return &ErrRep{Code: ErrCode(code), Message: msg}
}
type ErrRepInterface interface {
ErrRep() *ErrRep
}
type ErrCodeInterface interface {
}
type ErrCodeGeneric interface {
~int | ~int32 | ~int64 | ~uint | ~uint32 | ~uint64
}