-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathecode.go
150 lines (131 loc) · 3.65 KB
/
ecode.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
package ecode
import (
"errors"
"fmt"
"strconv"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/status"
)
// Error struct
type Error struct {
Status
cause error
}
func (e *Error) Error() string {
return fmt.Sprintf("error: code = %d reason = %s message = %s metadata = %v cause = %v", e.Status.Code, e.Status.Reason, e.Status.Message, e.Metadata, e.cause)
}
// Code returns the code of the error.
func (e *Error) Code() int { return int(e.Status.Code) }
// Message returns the message of the error.
func (e *Error) Message() string { return e.Status.Message }
// Reason returns the reason of the error.
func (e *Error) Reason() string { return e.Status.Reason }
// 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 {
if se := new(Error); errors.As(err, &se) {
return se.Status.Code == e.Status.Code && se.Status.Reason == e.Status.Reason
}
return false
}
// Equal matches error from code and reason.
func (e *Error) Equal(code int, reason ...string) bool {
se := &Error{Status: Status{
Code: int32(code),
}}
if len(reason) == 1 {
se.Status.Reason = reason[0]
return se.Status.Code == e.Status.Code && se.Status.Reason == e.Status.Reason
}
return se.Status.Code == e.Status.Code
}
// GRPCStatus returns the Status represented by error.
func (e *Error) GRPCStatus() *status.Status {
gs, _ := status.New(DefaultConverter.ToGRPCCode(int(e.Status.Code)), e.Status.Message).
WithDetails(&errdetails.ErrorInfo{Reason: e.Status.Reason, Metadata: e.Metadata})
return gs
}
// WithCause with the underlying cause of the error.
func (e *Error) WithCause(cause error) *Error {
err := DeepClone(e)
err.cause = cause
return err
}
// WithMetadata with an MD formed by the mapping of key, value.
func (e *Error) WithMetadata(md map[string]string) *Error {
err := DeepClone(e)
err.Metadata = md
return err
}
// ============================================================================================================
// New returns an error object for the code, message.
func New(code int, reason string, message string) *Error {
return &Error{Status: Status{
Code: int32(code),
Reason: reason,
Message: message,
}}
}
// DeepClone deep clone error to a new error.
func DeepClone(err *Error) *Error {
if err == nil {
return nil
}
metadata := make(map[string]string, len(err.Metadata))
for k, v := range err.Metadata {
metadata[k] = v
}
return &Error{
cause: err.cause,
Status: Status{
Code: err.Status.Code,
Reason: err.Status.Reason,
Message: err.Status.Message,
Metadata: metadata,
},
}
}
// FromError try to convert an error to *Error.
// It supports wrapped errors.
func FromError(err error) *Error {
if err == nil {
return Success
}
if se := new(Error); errors.As(err, &se) {
return se
}
gs, ok := status.FromError(err)
if !ok {
return New(UnknownCode, UnknownReason, err.Error())
}
ret := New(DefaultConverter.FromGRPCCode(gs.Code()), UnknownReason, gs.Message())
for _, detail := range gs.Details() {
switch d := detail.(type) {
case *errdetails.ErrorInfo:
ret.Status.Reason = d.Reason
return ret.WithMetadata(d.Metadata)
}
}
return ret
}
// AnalyseError analyse error info
func AnalyseError(err error) (e2 *Error) {
if err == nil {
return Success
}
if errors.As(err, &e2) {
return e2
}
return errStringToErrorV2(err.Error())
}
func errStringToErrorV2(e string) *Error {
if e == "" {
return Success
}
i, err := strconv.Atoi(e)
if err != nil {
return New(-1, UnknownReason, e)
}
return New(i, UnknownReason, e)
}