-
Notifications
You must be signed in to change notification settings - Fork 165
/
encoded_errors.go
274 lines (253 loc) · 9.08 KB
/
encoded_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
package errors
import (
"net/http"
"regexp"
"strings"
openfgav1 "github.com/openfga/api/proto/openfga/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const (
cFirstAuthenticationErrorCode int32 = 1000
cFirstValidationErrorCode int32 = 2000
cFirstInternalErrorCode int32 = 4000
cFirstUnknownEndpointErrorCode int32 = 5000
)
type ErrorResponse struct {
Code string `json:"code"`
Message string `json:"message"`
codeInt int32
}
// EncodedError allows customized error with code in string and specified http status field
type EncodedError struct {
HTTPStatusCode int
GRPCStatusCode codes.Code
ActualError ErrorResponse
}
// Error returns the encoded message
func (e *EncodedError) Error() string {
return e.ActualError.Message
}
// CodeValue returns the encoded code in integer
func (e *EncodedError) CodeValue() int32 {
return e.ActualError.codeInt
}
// HTTPStatus returns the HTTP Status code
func (e *EncodedError) HTTPStatus() int {
return e.HTTPStatusCode
}
func (e *EncodedError) GRPCStatus() *status.Status {
return status.New(e.GRPCStatusCode, e.Error())
}
// Code returns the encoded code in string
func (e *EncodedError) Code() string {
return e.ActualError.Code
}
func sanitizedMessage(message string) string {
parsedMessages := strings.Split(message, "| caused by:")
lastMessage := parsedMessages[len(parsedMessages)-1]
lastMessage = strings.TrimSpace(lastMessage)
sanitizedErrorMessage := regexp.MustCompile(`unexpected EOF`).ReplaceAllString(lastMessage, "malformed JSON")
sanitizedErrorMessage = regexp.MustCompile(`rpc error: code = [a-zA-Z0-9\(\)]* desc = `).ReplaceAllString(sanitizedErrorMessage, "")
return strings.TrimSpace(strings.TrimPrefix(sanitizedErrorMessage, "proto:"))
}
// NewEncodedError returns the encoded error with the correct http status code etc.
func NewEncodedError(errorCode int32, message string) *EncodedError {
if !IsValidEncodedError(errorCode) {
if errorCode == int32(codes.Aborted) {
return &EncodedError{
HTTPStatusCode: http.StatusConflict,
GRPCStatusCode: codes.Aborted,
ActualError: ErrorResponse{
Code: codes.Aborted.String(),
Message: sanitizedMessage(message),
codeInt: errorCode,
},
}
}
return &EncodedError{
HTTPStatusCode: http.StatusInternalServerError,
GRPCStatusCode: codes.Internal,
ActualError: ErrorResponse{
Code: openfgav1.InternalErrorCode(errorCode).String(),
Message: sanitizedMessage(message),
codeInt: errorCode,
},
}
}
var httpStatusCode int
var grpcStatusCode codes.Code
var code string
if errorCode >= cFirstAuthenticationErrorCode && errorCode < cFirstValidationErrorCode {
httpStatusCode = http.StatusUnauthorized
code = openfgav1.AuthErrorCode(errorCode).String()
grpcStatusCode = codes.Unauthenticated
} else if errorCode >= cFirstValidationErrorCode && errorCode < cFirstInternalErrorCode {
httpStatusCode = http.StatusBadRequest
code = openfgav1.ErrorCode(errorCode).String()
grpcStatusCode = codes.InvalidArgument
} else if errorCode >= cFirstInternalErrorCode && errorCode < cFirstUnknownEndpointErrorCode {
httpStatusCode = http.StatusInternalServerError
code = openfgav1.InternalErrorCode(errorCode).String()
grpcStatusCode = codes.Internal
} else {
httpStatusCode = http.StatusNotFound
code = openfgav1.NotFoundErrorCode(errorCode).String()
grpcStatusCode = codes.NotFound
}
return &EncodedError{
HTTPStatusCode: httpStatusCode,
GRPCStatusCode: grpcStatusCode,
ActualError: ErrorResponse{
Code: code,
Message: sanitizedMessage(message),
codeInt: errorCode,
},
}
}
// IsValidEncodedError returns whether the error code is a valid encoded error
func IsValidEncodedError(errorCode int32) bool {
return errorCode >= cFirstAuthenticationErrorCode
}
func getCustomizedErrorCode(field string, reason string) int32 {
switch field {
case "Assertions":
if strings.HasPrefix(reason, "value must contain no more than") {
return int32(openfgav1.ErrorCode_assertions_too_many_items)
}
case "AuthorizationModelId":
if strings.HasPrefix(reason, "value length must be at most") {
return int32(openfgav1.ErrorCode_authorization_model_id_too_long)
}
case "Base":
if strings.HasPrefix(reason, "value is required") {
return int32(openfgav1.ErrorCode_difference_base_missing_value)
}
case "Id":
if strings.HasPrefix(reason, "value length must be at most") {
return int32(openfgav1.ErrorCode_id_too_long)
}
case "Object":
if strings.HasPrefix(reason, "value length must be at most") {
return int32(openfgav1.ErrorCode_object_too_long)
}
case "PageSize":
if strings.HasPrefix(reason, "value must be inside range") {
return int32(openfgav1.ErrorCode_page_size_invalid)
}
case "Params":
if strings.HasPrefix(reason, "value is required") {
return int32(openfgav1.ErrorCode_param_missing_value)
}
case "Relation":
if strings.HasPrefix(reason, "value length must be at most") {
return int32(openfgav1.ErrorCode_relation_too_long)
}
case "Relations":
if strings.HasPrefix(reason, "value must contain at least") {
return int32(openfgav1.ErrorCode_relations_too_few_items)
}
case "Subtract":
if strings.HasPrefix(reason, "value is required") {
return int32(openfgav1.ErrorCode_subtract_base_missing_value)
}
case "StoreId":
if strings.HasPrefix(reason, "value length must be") {
return int32(openfgav1.ErrorCode_store_id_invalid_length)
}
case "TupleKey":
if strings.HasPrefix(reason, "value is required") {
return int32(openfgav1.ErrorCode_tuple_key_value_not_specified)
}
case "TupleKeys":
if strings.HasPrefix(reason, "value must contain between") {
return int32(openfgav1.ErrorCode_tuple_keys_too_many_or_too_few_items)
}
case "Type":
if strings.HasPrefix(reason, "value length must be at") {
return int32(openfgav1.ErrorCode_type_invalid_length)
}
if strings.HasPrefix(reason, "value does not match regex pattern") {
return int32(openfgav1.ErrorCode_type_invalid_pattern)
}
case "TypeDefinitions":
if strings.HasPrefix(reason, "value must contain at least") {
return int32(openfgav1.ErrorCode_type_definitions_too_few_items)
}
}
// We will need to check for regex pattern
if strings.HasPrefix(field, "Relations[") {
if strings.HasPrefix(reason, "value length must be at most") {
return int32(openfgav1.ErrorCode_relations_too_long)
}
if strings.HasPrefix(reason, "value does not match regex pattern") {
return int32(openfgav1.ErrorCode_relations_invalid_pattern)
}
}
// When we get to here, this is not a type or message that we know well.
// We needs to return the generic error type
return int32(openfgav1.ErrorCode_validation_error)
}
func ConvertToEncodedErrorCode(statusError *status.Status) int32 {
code := int32(statusError.Code())
if code >= cFirstAuthenticationErrorCode {
return code
}
switch statusError.Code() {
case codes.OK:
return int32(codes.OK)
case codes.Unauthenticated:
return int32(openfgav1.AuthErrorCode_unauthenticated)
case codes.Canceled:
return int32(openfgav1.InternalErrorCode_cancelled)
case codes.Unknown:
// we will return InternalError as our implementation of
// InternalError does not have a status code - which will result
// in unknown error
return int32(openfgav1.InternalErrorCode_internal_error)
case codes.DeadlineExceeded:
return int32(openfgav1.InternalErrorCode_deadline_exceeded)
case codes.NotFound:
return int32(openfgav1.NotFoundErrorCode_undefined_endpoint)
case codes.AlreadyExists:
return int32(openfgav1.InternalErrorCode_already_exists)
case codes.ResourceExhausted:
return int32(openfgav1.InternalErrorCode_resource_exhausted)
case codes.FailedPrecondition:
return int32(openfgav1.InternalErrorCode_failed_precondition)
case codes.Aborted:
return int32(codes.Aborted)
case codes.OutOfRange:
return int32(openfgav1.InternalErrorCode_out_of_range)
case codes.Unimplemented:
return int32(openfgav1.NotFoundErrorCode_unimplemented)
case codes.Internal:
return int32(openfgav1.InternalErrorCode_internal_error)
case codes.Unavailable:
return int32(openfgav1.InternalErrorCode_unavailable)
case codes.DataLoss:
return int32(openfgav1.InternalErrorCode_data_loss)
case codes.InvalidArgument:
break
default:
// Unknown code - internal error
return int32(openfgav1.InternalErrorCode_internal_error)
}
// When we get to here, the cause is InvalidArgument (likely flagged by the framework's validator).
// We will try to find out the actual cause if possible. Otherwise, the default response will
// be openfgav1.ErrorCode_validation_error
lastMessage := sanitizedMessage(statusError.Message())
lastMessageSplitted := strings.SplitN(lastMessage, ": ", 2)
if len(lastMessageSplitted) < 2 {
// I don't know how to process this message.
// The safest thing is to return the generic validation error
return int32(openfgav1.ErrorCode_validation_error)
}
errorObjectSplitted := strings.Split(lastMessageSplitted[0], ".")
if len(errorObjectSplitted) != 2 {
// I don't know is the type.
// Return generic error type
return int32(openfgav1.ErrorCode_validation_error)
}
return getCustomizedErrorCode(errorObjectSplitted[1], lastMessageSplitted[1])
}