/
error.go
182 lines (151 loc) · 3.71 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
package errorw
import (
"errors"
"fmt"
pkgerrors "github.com/pkg/errors"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Error wrap error with fields and stack
type Error struct {
Err error
Stack *stack
Wrapper []string
Fields map[string]interface{}
APIErrors []*status.Status
}
type causer interface {
Cause() error
}
// Verify interface compliance at compile time
var _ error = (*Error)(nil)
var _ stackTracer = (*Error)(nil)
var _ causer = (*Error)(nil)
var _ zapcore.ObjectMarshaler = (*Error)(nil)
var _ interface{ GRPCStatus() *status.Status } = (*Error)(nil)
func (e *Error) Error() string {
return Render(e)
}
// Cause implement errors.Cause interface.
func (e *Error) Cause() error {
return pkgerrors.Cause(e.Err)
}
// APIErrorCause return the root cause of the API error.
func (e *Error) APIErrorCause() *status.Status {
if len(e.APIErrors) > 0 {
return e.APIErrors[0]
}
return nil
}
// GRPCStatus return the internal error's gRPC status or the root cause of the API error.
// Priority returns the internal error's gRPC status if it implements status.GRPCStatus.
// If no gRPC status can be use, then create a gRPC status with internal error.
// Implement gRPC status.GRPCStatus function.
func (e *Error) GRPCStatus() *status.Status {
if e.Err != nil {
if se, ok := e.Err.(interface {
GRPCStatus() *status.Status
}); ok {
return se.GRPCStatus()
}
}
st := e.APIErrorCause()
if st != nil {
return st
}
if e.Err != nil {
return status.New(codes.Internal, e.Err.Error())
}
return nil
}
// WithAPIError append API error to error
func (e *Error) WithAPIError(apiError *status.Status) *Error {
if e == nil {
return nil
}
e.APIErrors = append(e.APIErrors, apiError)
return e
}
// WithField append key/value to error
func (e *Error) WithField(key string, value interface{}) *Error {
if e == nil {
return nil
}
if e.Fields == nil {
e.Fields = make(map[string]interface{})
}
e.Fields[key] = value
return e
}
// WithFields append fields to error.
// Parameter fields will cover value which key is already exists.
func (e *Error) WithFields(fields map[string]interface{}) *Error {
if e == nil {
return nil
}
if e.Fields == nil {
e.Fields = fields
} else {
for k, v := range fields {
e.Fields[k] = v
}
}
return e
}
// WithWrap wrap message to error
func (e *Error) WithWrap(message string) *Error {
if e == nil {
return nil
}
e.Wrapper = append(e.Wrapper, message)
return e
}
// New create an error
func New(err error) *Error {
return newError(err, 4)
}
func newError(err error, skip int) *Error {
if err == nil {
return nil
}
return &Error{
Err: err,
Stack: callers(skip),
}
}
// Wrap wrap message
func Wrap(err error, message string) *Error {
if err == nil {
return nil
}
if val, ok := err.(*Error); ok {
return val.WithWrap(message)
}
return New(err).WithWrap(message)
}
// Wrapf wrap message with formats.
func Wrapf(err error, format string, args ...interface{}) *Error {
if err == nil {
return nil
}
message := fmt.Sprintf(format, args...)
if val, ok := err.(*Error); ok {
return val.WithWrap(message)
}
return New(err).WithWrap(message)
}
// NewMessage create an error with message.
func NewMessage(message string) *Error {
return newError(errors.New(message), 4)
}
// NewMessagef create an error with message.
// It will formats according to a format specifier and returns the resulting string.
func NewMessagef(format string, args ...interface{}) *Error {
return newError(fmt.Errorf(format, args...), 4)
}
// NewAPIError create an error and append API error.
func NewAPIError(apiError *status.Status) *Error {
return newError(errors.New(apiError.Message()), 4).
WithAPIError(apiError)
}