forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
337 lines (300 loc) · 10.9 KB
/
errors.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package gateway
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"strconv"
"strings"
"sync/atomic"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/infobloxopen/atlas-app-toolkit/rpc/errdetails"
"github.com/infobloxopen/atlas-app-toolkit/rpc/errfields"
)
// ProtoStreamErrorHandlerFunc handles the error as a gRPC error generated via status package and replies to the testRequest.
// Addition bool argument indicates whether method (http.ResponseWriter.WriteHeader) was called or not.
type ProtoStreamErrorHandlerFunc func(context.Context, bool, *runtime.ServeMux, runtime.Marshaler, http.ResponseWriter, *http.Request, error)
type RestErrs struct {
Error []map[string]interface{} `json:"error,omitempty"`
}
var (
// ProtoMessageErrorHandler uses PrefixOutgoingHeaderMatcher.
// To use ProtoErrorHandler with custom outgoing header matcher call NewProtoMessageErrorHandler.
ProtoMessageErrorHandler = NewProtoMessageErrorHandler(PrefixOutgoingHeaderMatcher)
// ProtoStreamErrorHandler uses PrefixOutgoingHeaderMatcher.
// To use ProtoErrorHandler with custom outgoing header matcher call NewProtoStreamErrorHandler.
ProtoStreamErrorHandler = NewProtoStreamErrorHandler(PrefixOutgoingHeaderMatcher)
)
// NewProtoMessageErrorHandler returns runtime.ProtoErrorHandlerFunc
func NewProtoMessageErrorHandler(out runtime.HeaderMatcherFunc) runtime.ErrorHandlerFunc {
h := &ProtoErrorHandler{out}
return h.MessageHandler
}
// NewProtoStreamErrorHandler returns ProtoStreamErrorHandlerFunc
func NewProtoStreamErrorHandler(out runtime.HeaderMatcherFunc) ProtoStreamErrorHandlerFunc {
h := &ProtoErrorHandler{out}
return h.StreamHandler
}
// ProtoErrorHandler implements runtime.ProtoErrorHandlerFunc in method MessageHandler
// and ProtoStreamErrorHandlerFunc in method StreamHandler
// in accordance with REST API Syntax Specification.
// See RestError for the JSON format of an error
type ProtoErrorHandler struct {
OutgoingHeaderMatcher runtime.HeaderMatcherFunc
}
// MessageHandler implements runtime.ProtoErrorHandlerFunc
// in accordance with REST API Syntax Specification.
// See RestError for the JSON format of an error
func (h *ProtoErrorHandler) MessageHandler(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, rw http.ResponseWriter, req *http.Request, err error) {
md, ok := runtime.ServerMetadataFromContext(ctx)
if !ok {
grpclog.Infof("error handler: failed to extract ServerMetadata from context")
}
handleForwardResponseServerMetadata(h.OutgoingHeaderMatcher, rw, md)
handleForwardResponseTrailerHeader(rw, md)
h.writeError(ctx, false, marshaler, rw, err)
handleForwardResponseTrailer(rw, md)
}
// StreamHandler implements ProtoStreamErrorHandlerFunc
// in accordance with REST API Syntax Specification.
// See RestError for the JSON format of an error
func (h *ProtoErrorHandler) StreamHandler(ctx context.Context, headerWritten bool, mux *runtime.ServeMux, marshaler runtime.Marshaler, rw http.ResponseWriter, req *http.Request, err error) {
h.writeError(ctx, headerWritten, marshaler, rw, err)
}
func (h *ProtoErrorHandler) writeError(ctx context.Context, headerWritten bool, marshaler runtime.Marshaler, rw http.ResponseWriter, err error) {
var fallback = `{"error":[{"message":"%s"}]}`
if setStatusDetails {
fallback = `{"error":[{"message":"%s", "code":500, "status": "INTERNAL"}]}`
}
st, ok := status.FromError(err)
if !ok {
st = status.New(codes.Unknown, err.Error())
}
statusCode, statusStr := HTTPStatus(ctx, st)
details := []interface{}{}
var fields interface{}
for _, d := range st.Details() {
switch d.(type) {
case *errdetails.TargetInfo:
details = append(details, d)
case *errfields.FieldInfo:
fields = d
default:
grpclog.Infof("error handler: failed to recognize error message")
rw.WriteHeader(http.StatusInternalServerError)
return
}
}
restErr := map[string]interface{}{
"message": st.Message(),
}
if len(details) > 0 {
restErr["details"] = details
}
if fields != nil {
restErr["fields"] = fields
}
if setStatusDetails {
restErr["code"] = statusCode
restErr["status"] = statusStr
}
errs, _, overrideErr := errorsAndSuccessFromContext(ctx)
restResp := &RestErrs{
Error: errs,
}
if !overrideErr {
restResp.Error = append(restResp.Error, restErr)
} else if setStatusDetails && len(restResp.Error) > 0 {
restResp.Error[0]["code"] = statusCode
restResp.Error[0]["status"] = statusStr
}
if !headerWritten {
rw.Header().Del("Trailer")
rw.Header().Set("Content-Type", marshaler.ContentType(nil))
rw.WriteHeader(statusCode)
}
buf, merr := marshaler.Marshal(restResp)
if merr != nil {
grpclog.Infof("error handler: failed to marshal error message %q: %v", restErr, merr)
rw.WriteHeader(http.StatusInternalServerError)
if _, err := io.WriteString(rw, fmt.Sprintf(fallback, merr)); err != nil {
grpclog.Infof("error handler: failed to write response: %v", err)
}
return
}
if _, err := rw.Write(buf); err != nil {
grpclog.Infof("error handler: failed to write response: %v", err)
}
}
// For small performance bump, switch map[string]string to a tuple-type (string, string)
type MessageWithFields interface {
error
GetFields() map[string]interface{}
GetMessage() string
}
type messageWithFields struct {
message string
fields map[string]interface{}
}
func (m *messageWithFields) Error() string {
return m.message
}
func (m *messageWithFields) GetFields() map[string]interface{} {
return m.fields
}
func (m *messageWithFields) GetMessage() string {
return m.message
}
// NewWithFields returns a new MessageWithFields that requires a message string,
// and then treats the following arguments as alternating keys and values
// a non-string key will immediately return the result so far, ignoring later
// values. The values can be any type
func NewWithFields(message string, kvpairs ...interface{}) MessageWithFields {
mwf := &messageWithFields{message: message, fields: make(map[string]interface{})}
for i := 0; i+1 < len(kvpairs); i += 2 {
k, ok := kvpairs[i].(string)
if !ok {
return mwf
}
mwf.fields[k] = kvpairs[i+1]
}
return mwf
}
// For giving each error a unique metadata key, but not leaking the exact count
// of errors or something like that
var counter *uint32
func init() {
counter = new(uint32)
*counter = uint32(time.Now().Nanosecond() % math.MaxUint32)
}
// WithError will save an error message into the grpc trailer metadata, if it
// is an error that implements MessageWithFields, it also saves the fields.
// This error will then be inserted into the return JSON if the ResponseForwarder
// is used
func WithError(ctx context.Context, err error) {
i := atomic.AddUint32(counter, uint32(time.Now().Nanosecond()%100+1))
md := metadata.Pairs(fmt.Sprintf("error-%d", i), fmt.Sprintf("message:%s", err.Error()))
if mwf, ok := err.(MessageWithFields); ok {
if f := mwf.GetFields(); f != nil {
b, _ := json.Marshal(mwf.GetFields())
md.Append(fmt.Sprintf("error-%d", i), fmt.Sprintf("fields:%q", b))
}
}
grpc.SetTrailer(ctx, md)
}
// NewResponseError sets the error in the context with extra fields, to
// override the standard message-only error
func NewResponseError(ctx context.Context, msg string, kvpairs ...interface{}) error {
md := metadata.Pairs("error", fmt.Sprintf("message:%s", msg))
if len(kvpairs) > 0 {
fields := make(map[string]interface{})
for i := 0; i+1 < len(kvpairs); i += 2 {
k, ok := kvpairs[i].(string)
if !ok {
grpclog.Infof("Key value for error details must be a string")
continue
}
fields[k] = kvpairs[i+1]
}
b, _ := json.Marshal(fields)
md.Append("error", fmt.Sprintf("fields:%q", b))
}
grpc.SetTrailer(ctx, md)
return errors.New(msg) // Message should be overridden in response writer
}
// NewResponseErrorWithCode sets the return code and returns an error with extra
// fields in MD to be extracted in the gateway response writer
func NewResponseErrorWithCode(ctx context.Context, c codes.Code, msg string, kvpairs ...interface{}) error {
SetStatus(ctx, status.New(c, msg))
NewResponseError(ctx, msg, kvpairs...)
return status.Error(c, msg)
}
// WithSuccess will save a MessageWithFields into the grpc trailer metadata.
// This success message will then be inserted into the return JSON if the
// ResponseForwarder is used
func WithSuccess(ctx context.Context, msg MessageWithFields) {
i := atomic.AddUint32(counter, uint32(time.Now().Nanosecond()%100+1))
md := metadata.Pairs(fmt.Sprintf("success-%d", i), fmt.Sprintf("message:%s", msg.Error()))
if f := msg.GetFields(); f != nil {
b, _ := json.Marshal(msg.GetFields())
md.Append(fmt.Sprintf("success-%d", i), fmt.Sprintf("fields:%q", b))
}
grpc.SetTrailer(ctx, md)
}
// WithCodedSuccess wraps a SetStatus and WithSuccess call into one, just to make things a little more "elegant"
func WithCodedSuccess(ctx context.Context, c codes.Code, msg string, args ...interface{}) error {
WithSuccess(ctx, NewWithFields(msg, args))
return SetStatus(ctx, status.New(c, msg))
}
func errorsAndSuccessFromContext(ctx context.Context) (errors []map[string]interface{}, success map[string]interface{}, errorOverride bool) {
md, ok := runtime.ServerMetadataFromContext(ctx)
if !ok {
return nil, nil, false
}
errors = make([]map[string]interface{}, 0)
var primaryError map[string]interface{}
latestSuccess := int64(-1)
for k, vs := range md.TrailerMD {
if k == "error" {
err := make(map[string]interface{})
for _, v := range vs {
parts := strings.SplitN(v, ":", 2)
if parts[0] == "fields" {
uq, _ := strconv.Unquote(parts[1])
json.Unmarshal([]byte(uq), &err)
} else if parts[0] == "message" {
err["message"] = parts[1]
}
}
primaryError = err
errorOverride = true
}
if strings.HasPrefix(k, "error-") {
err := make(map[string]interface{})
for _, v := range vs {
parts := strings.SplitN(v, ":", 2)
if parts[0] == "fields" {
uq, _ := strconv.Unquote(parts[1])
json.Unmarshal([]byte(uq), &err)
} else if parts[0] == "message" {
err["message"] = parts[1]
}
}
errors = append(errors, err)
}
if num := strings.TrimPrefix(k, "success-"); num != k {
// Let the later success messages override previous ones,
// also account for the possiblity of wraparound with a generous check
if i, err := strconv.ParseInt(num, 10, 32); err == nil {
if i > latestSuccess || (i < 1<<12 && latestSuccess > 1<<28) {
latestSuccess = i
} else {
continue
}
}
success = make(map[string]interface{})
for _, v := range vs {
parts := strings.SplitN(v, ":", 2)
if parts[0] == "fields" {
uq, _ := strconv.Unquote(parts[1])
json.Unmarshal([]byte(uq), &success)
} else if parts[0] == "message" {
success["message"] = parts[1]
}
}
}
}
if errorOverride {
errors = append([]map[string]interface{}{primaryError}, errors...)
}
return
}