/
errors.go
317 lines (285 loc) · 10.5 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
package handlers
import (
"context"
"fmt"
"io"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api"
pberrors "github.com/hashicorp/boundary/internal/gen/errors"
"github.com/hashicorp/boundary/internal/observability/event"
"github.com/mr-tron/base58"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
const (
genericUniquenessMsg = "Invalid request. Request attempted to make second resource with the same field value that must be unique."
genericNotFoundMsg = "Unable to find requested resource."
// domainErrHeader defines an http header for encoded domain errors from the
// grpc server.
domainErrHeader = "x-domain-err"
// domainErrMetadataHeader defines an http header for encoded domain errors from the
// grpc server via metadata
domainErrMetadataHeader = "Grpc-Metadata-X-Domain-Err"
apiErrHeader = "x-api-err"
apiErrMetadataHeader = "Grpc-Metadata-X-Api-Err"
)
type ApiError struct {
Status int32
Inner *pb.Error
}
func (e *ApiError) Error() string {
res := fmt.Sprintf("Status: %d, Kind: %q, Error: %q", e.Status, e.Inner.GetKind(), e.Inner.GetMessage())
var dets []string
for _, rf := range e.Inner.GetDetails().GetRequestFields() {
dets = append(dets, fmt.Sprintf("{name: %q, desc: %q}", rf.GetName(), rf.GetDescription()))
}
if len(dets) > 0 {
det := strings.Join(dets, ", ")
res = fmt.Sprintf("%s, Details: {%s}", res, det)
}
return res
}
func (e *ApiError) Is(target error) bool {
var tApiErr *ApiError
if !errors.As(target, &tApiErr) {
return false
}
return tApiErr.Inner.Kind == e.Inner.Kind && tApiErr.Status == e.Status
}
// ApiErrorWithCode returns an api error with the provided code.
func ApiErrorWithCode(c codes.Code) error {
return &ApiError{
Status: int32(runtime.HTTPStatusFromCode(c)),
Inner: &pb.Error{
Kind: c.String(),
},
}
}
// ApiErrorWithCodeAndMessage returns an api error with the provided code and message.
func ApiErrorWithCodeAndMessage(c codes.Code, msg string, args ...interface{}) error {
return &ApiError{
Status: int32(runtime.HTTPStatusFromCode(c)),
Inner: &pb.Error{
Kind: c.String(),
Message: fmt.Sprintf(msg, args...),
},
}
}
// NotFoundError returns an ApiError indicating a resource couldn't be found.
func NotFoundError() error {
return &ApiError{
Status: http.StatusNotFound,
Inner: &pb.Error{
Kind: codes.NotFound.String(),
Message: "Resource not found.",
},
}
}
// NotFoundErrorf returns an ApiError indicating a resource couldn't be found.
func NotFoundErrorf(msg string, a ...interface{}) *ApiError {
return &ApiError{
Status: http.StatusNotFound,
Inner: &pb.Error{
Kind: codes.NotFound.String(),
Message: fmt.Sprintf(msg, a...),
},
}
}
var unauthorizedError = &ApiError{
Status: http.StatusForbidden,
Inner: &pb.Error{
Kind: codes.PermissionDenied.String(),
Message: "Forbidden.",
},
}
func ForbiddenError() error {
return unauthorizedError
}
var unauthenticatedError = &ApiError{
Status: http.StatusUnauthorized,
Inner: &pb.Error{
Kind: codes.Unauthenticated.String(),
Message: "Unauthenticated, or invalid token.",
},
}
func UnauthenticatedError() error {
return unauthenticatedError
}
func InvalidArgumentErrorf(msg string, fields map[string]string) *ApiError {
const op = "handlers.InvalidArgumentErrorf"
ctx := context.TODO()
err := ApiErrorWithCodeAndMessage(codes.InvalidArgument, msg)
var apiErr *ApiError
if !errors.As(err, &apiErr) {
event.WriteError(ctx, op, err, event.WithInfoMsg("Unable to build invalid argument api error."))
}
if len(fields) > 0 {
apiErr.Inner.Details = &pb.ErrorDetails{}
}
for k, v := range fields {
apiErr.Inner.Details.RequestFields = append(apiErr.Inner.Details.RequestFields, &pb.FieldError{Name: k, Description: v})
}
sort.Slice(apiErr.Inner.GetDetails().GetRequestFields(), func(i, j int) bool {
return apiErr.Inner.Details.RequestFields[i].GetName() < apiErr.Inner.Details.RequestFields[j].GetName()
})
return apiErr
}
var statusRegEx = regexp.MustCompile("Status: ([0-9]+), Kind: \"(.*)\", Error: \"(.*)\"")
// Converts a known errors into an error that can presented to an end user over the API.
func backendErrorToApiError(inErr error) *ApiError {
stErr := status.Convert(inErr)
switch {
case errors.Is(inErr, runtime.ErrNotMatch):
// grpc gateway uses this error when the path was not matched, but the error uses codes.Unimplemented which doesn't match the intention.
// Overwrite the error to match our expected behavior.
return &ApiError{
Status: http.StatusNotFound,
Inner: &pb.Error{
Kind: codes.NotFound.String(),
Message: http.StatusText(http.StatusNotFound),
},
}
case status.Code(inErr) == codes.Unimplemented:
// Instead of returning a 501 we always want to return a 405 when a method isn't implemented.
return &ApiError{
Status: http.StatusMethodNotAllowed,
Inner: &pb.Error{
Kind: codes.Unimplemented.String(),
Message: stErr.Message(),
},
}
case errors.Match(errors.T(errors.RecordNotFound), inErr):
return NotFoundErrorf(genericNotFoundMsg)
case errors.Match(errors.T(errors.AccountAlreadyAssociated), inErr):
return InvalidArgumentErrorf(inErr.Error(), nil)
case errors.Match(errors.T(errors.InvalidFieldMask), inErr), errors.Match(errors.T(errors.EmptyFieldMask), inErr):
return InvalidArgumentErrorf("Error in provided request", map[string]string{"update_mask": "Invalid update mask provided."})
case errors.IsUniqueError(inErr):
return InvalidArgumentErrorf(genericUniquenessMsg, nil)
}
var statusCode int32 = http.StatusInternalServerError
var domainErr *errors.Err
if errors.As(inErr, &domainErr) && domainErr.Code >= 400 && domainErr.Code <= 599 {
// Domain error codes 400-599 align with http client and server error codes, use the domain error code instead of 500
statusCode = int32(domainErr.Code)
}
// perhaps the error is from the grpc gateway, so match against the known
// apiError msg format.
if found := statusRegEx.FindStringSubmatch(stErr.Message()); len(found) == 4 {
u32, err := strconv.ParseInt(found[1], 10, 32)
if err == nil { // notice it's testing for NO err
return &ApiError{
Status: int32(u32),
Inner: &pb.Error{Kind: found[2], Message: found[3]},
}
}
}
// TODO: Don't return potentially sensitive information (like which user id an account
// is already associated with when attempting to re-associate it).
return &ApiError{
Status: statusCode,
Inner: &pb.Error{Kind: codes.Internal.String(), Message: inErr.Error()},
}
}
func ErrorHandler() runtime.ErrorHandlerFunc {
const op = "handlers.ErrorHandler"
const errorFallback = `{"error": "failed to marshal error message"}`
return func(ctx context.Context, _ *runtime.ServeMux, mar runtime.Marshaler, w http.ResponseWriter, r *http.Request, inErr error) {
// API specified error, otherwise we need to translate repo/db errors.
// the grpc server will encoded domain errors into the x-domain-err, so
// let's check there first. (see: controller.errorInterceptor)
md, ok := runtime.ServerMetadataFromContext(ctx)
if ok {
defer func() {
// make sure we don't leak the headers that were used as a comm
// channel between the grpc server and the http proxy
delete(md.HeaderMD, domainErrHeader)
delete(w.Header(), domainErrMetadataHeader)
delete(md.HeaderMD, apiErrHeader)
delete(w.Header(), apiErrMetadataHeader)
}()
domainErrHdrs := md.HeaderMD.Get(domainErrHeader)
apiErrHdrs := md.HeaderMD.Get(apiErrHeader)
switch {
case len(domainErrHdrs) > 0:
decoded, err := base58.FastBase58Decoding(domainErrHdrs[0])
if err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to decode domain err header"))
w.WriteHeader(http.StatusInternalServerError)
if _, err := io.WriteString(w, errorFallback); err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to write response"))
}
return
}
var pbErr pberrors.Err
if err := proto.Unmarshal(decoded, &pbErr); err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to marshal domain err header"))
w.WriteHeader(http.StatusInternalServerError)
if _, err := io.WriteString(w, errorFallback); err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to write response"))
}
return
}
inErr = errors.FromPbErrors(&pbErr)
case len(apiErrHdrs) > 0:
decoded, err := base58.FastBase58Decoding(apiErrHdrs[0])
if err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to decode api err header"))
w.WriteHeader(http.StatusInternalServerError)
if _, err := io.WriteString(w, errorFallback); err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to write response"))
}
return
}
var pbApiErr pberrors.ApiError
if err := proto.Unmarshal(decoded, &pbApiErr); err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to marshal api err header"))
w.WriteHeader(http.StatusInternalServerError)
if _, err := io.WriteString(w, errorFallback); err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to write response"))
}
return
}
inErr = &ApiError{
Status: pbApiErr.Status,
Inner: pbApiErr.ApiError,
}
}
}
var apiErr *ApiError
isApiErr := errors.As(inErr, &apiErr)
if !isApiErr {
if err := backendErrorToApiError(inErr); err != nil && !errors.As(err, &apiErr) {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to cast error to api error"))
}
}
if apiErr.Status == http.StatusInternalServerError {
event.WriteError(ctx, op, inErr, event.WithInfoMsg("internal error returned"))
}
buf, merr := mar.Marshal(apiErr.Inner)
if merr != nil {
event.WriteError(ctx, op, merr, event.WithInfoMsg("failed to marshal error response", "response", fmt.Sprintf("%#v", apiErr.Inner)))
w.WriteHeader(http.StatusInternalServerError)
if _, err := io.WriteString(w, errorFallback); err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to write response"))
}
return
}
w.Header().Set("Content-Type", mar.ContentType(apiErr.Inner))
w.WriteHeader(int(apiErr.Status))
if _, err := w.Write(buf); err != nil {
event.WriteError(ctx, op, err, event.WithInfoMsg("failed to send response chunk"))
return
}
}
}
func ToApiError(e error) *pb.Error {
return backendErrorToApiError(e).Inner
}