Change Format of API Error (#784)

This is a wire breaking change for json errors since we have removed and renamed a few fields, and changed part of the structure of the returned json error.

* Remove redundant fields in Status, details.error_id, details.TraceId and details.request_id from API errors.

* Added wrapped errors and "ops" field to indicate the internal operation of the errors returned by the API.

* Added error cli error reporting of Op and wrapped errors.
This commit is contained in:
Todd Knight authored and GitHub committed 2020-12-07 14:25:16 -07:00
1 parent 18ea39b237
commit aa4157639c
48 files changed
+742 -728

No files matched your search

+13 -5
View File
@@ -10,7 +10,6 @@ import (
"time"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/globals"
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/gen/controller/api/services"
@@ -22,6 +21,8 @@ import (
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/shared-secure-libs/configutil"
"github.com/hashicorp/vault/sdk/helper/base62"
"google.golang.org/grpc/codes"
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/authtokens"
@@ -175,6 +176,16 @@ func handleGrpcGateway(c *Controller, props HandlerProperties) (http.Handler, er
return mux, nil
}
// generatedTraceId returns a boundary generated TraceId or "" if an error occurs when generating
// the id.
func generatedTraceId() string {
t, err := base62.Random(20)
if err != nil {
return ""
}
return fmt.Sprintf("gtraceid_%s", t)
}
func wrapHandlerWithCommonFuncs(h http.Handler, c *Controller, props HandlerProperties) http.Handler {
var maxRequestDuration time.Duration
var maxRequestSize int64
@@ -278,10 +289,7 @@ func wrapHandlerWithCors(h http.Handler, props HandlerProperties) http.Handler {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusForbidden)
err := &api.Error{
Status: http.StatusForbidden,
Code: "origin forbidden",
}
err := handlers.ApiErrorWithCodeAndMessage(codes.PermissionDenied, "origin forbidden")
enc := json.NewEncoder(w)
enc.Encode(err)
+60 -67
View File
@@ -12,7 +12,6 @@ import (
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/helper/base62"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
@@ -23,11 +22,12 @@ const (
)
type apiError struct {
inner *pb.Error
status int32
inner *pb.Error
}
func (e *apiError) Error() string {
res := fmt.Sprintf("Status: %d, Code: %q, Error: %q", e.inner.GetStatus(), e.inner.GetCode(), e.inner.GetMessage())
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()))
@@ -44,58 +44,64 @@ func (e *apiError) Is(target error) bool {
if !errors.As(target, &tApiErr) {
return false
}
return tApiErr.inner.Code == e.inner.Code && tApiErr.inner.Status == e.inner.Status
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{inner: &pb.Error{
Status: int32(runtime.HTTPStatusFromCode(c)),
Code: c.String(),
}}
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{inner: &pb.Error{
Status: int32(runtime.HTTPStatusFromCode(c)),
Code: c.String(),
Message: fmt.Sprintf(msg, args...),
}}
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{&pb.Error{
Status: http.StatusNotFound,
Code: codes.NotFound.String(),
Message: "Resource not found.",
}}
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{}) error {
return &apiError{&pb.Error{
Status: http.StatusNotFound,
Code: codes.NotFound.String(),
Message: fmt.Sprintf(msg, a...),
}}
return &apiError{
status: http.StatusNotFound,
inner: &pb.Error{
Kind: codes.NotFound.String(),
Message: fmt.Sprintf(msg, a...),
}}
}
func ForbiddenError() error {
return &apiError{&pb.Error{
Status: http.StatusForbidden,
Code: codes.PermissionDenied.String(),
Message: "Forbidden.",
}}
return &apiError{
status: http.StatusForbidden,
inner: &pb.Error{
Kind: codes.PermissionDenied.String(),
Message: "Forbidden.",
}}
}
func UnauthenticatedError() error {
return &apiError{&pb.Error{
Status: http.StatusUnauthorized,
Code: codes.Unauthenticated.String(),
Message: "Unauthenticated, or invalid token.",
}}
return &apiError{
status: http.StatusUnauthorized,
inner: &pb.Error{
Kind: codes.Unauthenticated.String(),
Message: "Unauthenticated, or invalid token.",
}}
}
func InvalidArgumentErrorf(msg string, fields map[string]string) error {
@@ -125,18 +131,20 @@ func backendErrorToApiError(inErr error) error {
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{inner: &pb.Error{
Status: http.StatusNotFound,
Code: codes.NotFound.String(),
Message: http.StatusText(http.StatusNotFound),
}}
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{inner: &pb.Error{
Status: http.StatusMethodNotAllowed,
Code: codes.Unimplemented.String(),
Message: stErr.Message(),
}}
return &apiError{
status: http.StatusMethodNotAllowed,
inner: &pb.Error{
Kind: codes.Unimplemented.String(),
Message: stErr.Message(),
}}
case errors.Is(inErr, errors.ErrRecordNotFound):
return NotFoundErrorf(genericNotFoundMsg)
case errors.Is(inErr, errors.ErrInvalidFieldMask), errors.Is(inErr, errors.ErrEmptyFieldMask):
@@ -144,23 +152,14 @@ func backendErrorToApiError(inErr error) error {
case errors.IsUniqueError(inErr), errors.Is(inErr, errors.ErrNotUnique):
return InvalidArgumentErrorf(genericUniquenessMsg, nil)
}
return nil
}
func getInternalError(id string) *apiError {
return &apiError{&pb.Error{
Status: http.StatusInternalServerError,
Code: codes.Internal.String(),
Details: &pb.ErrorDetails{ErrorId: id},
}}
}
func internalErrorId() (string, error) {
errId, err := base62.Random(10)
if err != nil {
return "", fmt.Errorf("unable to generate id: %w", err)
// We haven't been able to identify what this backend error is, return it as an internal error
// 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: http.StatusInternalServerError,
inner: &pb.Error{Kind: codes.Internal.String(), Message: inErr.Error()},
}
return errId, nil
}
func ErrorHandler(logger hclog.Logger) runtime.ErrorHandlerFunc {
@@ -175,14 +174,8 @@ func ErrorHandler(logger hclog.Logger) runtime.ErrorHandlerFunc {
}
}
if apiErr == nil || apiErr.inner.GetStatus() == http.StatusInternalServerError {
errId, err := internalErrorId()
if err != nil {
logger.Error("unable to generate internal error id", "error", err)
errId = "failed_to_generate_error_id"
}
logger.Error("internal error returned", "error id", errId, "error", inErr)
apiErr = getInternalError(errId)
if apiErr.status == http.StatusInternalServerError {
logger.Error("internal error returned", "error", inErr)
}
buf, merr := mar.Marshal(apiErr.inner)
@@ -196,7 +189,7 @@ func ErrorHandler(logger hclog.Logger) runtime.ErrorHandlerFunc {
}
w.Header().Set("Content-Type", mar.ContentType(apiErr.inner))
w.WriteHeader(int(apiErr.inner.GetStatus()))
w.WriteHeader(int(apiErr.status))
if _, err := w.Write(buf); err != nil {
logger.Error("failed to send response chunk", "error", err)
return
@@ -33,15 +33,17 @@ func TestApiErrorHandler(t *testing.T) {
testCases := []struct {
name string
err error
expected *pb.Error
expected apiError
}{
{
name: "Not Found",
err: NotFoundErrorf("Test"),
expected: &pb.Error{
Status: http.StatusNotFound,
Code: "NotFound",
Message: "Test",
expected: apiError{
status: http.StatusNotFound,
inner: &pb.Error{
Kind: "NotFound",
Message: "Test",
},
},
},
{
@@ -50,19 +52,21 @@ func TestApiErrorHandler(t *testing.T) {
"k1": "v1",
"k2": "v2",
}),
expected: &pb.Error{
Status: http.StatusBadRequest,
Code: "InvalidArgument",
Message: "Test",
Details: &pb.ErrorDetails{
RequestFields: []*pb.FieldError{
{
Name: "k1",
Description: "v1",
},
{
Name: "k2",
Description: "v2",
expected: apiError{
status: http.StatusBadRequest,
inner: &pb.Error{
Kind: "InvalidArgument",
Message: "Test",
Details: &pb.ErrorDetails{
RequestFields: []*pb.FieldError{
{
Name: "k1",
Description: "v1",
},
{
Name: "k2",
Description: "v2",
},
},
},
},
@@ -71,93 +75,113 @@ func TestApiErrorHandler(t *testing.T) {
{
name: "GrpcGateway Routing Error",
err: runtime.ErrNotMatch,
expected: &pb.Error{
Status: http.StatusNotFound,
Code: "NotFound",
Message: http.StatusText(http.StatusNotFound),
expected: apiError{
status: http.StatusNotFound,
inner: &pb.Error{
Kind: "NotFound",
Message: http.StatusText(http.StatusNotFound),
},
},
},
{
name: "Unimplemented error",
err: status.Error(codes.Unimplemented, "Test"),
expected: &pb.Error{
Status: http.StatusMethodNotAllowed,
Code: "Unimplemented",
Message: "Test",
expected: apiError{
status: http.StatusMethodNotAllowed,
inner: &pb.Error{
Kind: "Unimplemented",
Message: "Test",
},
},
},
{
name: "Unknown error",
err: stderrors.New("Some random error"),
expected: &pb.Error{
Status: http.StatusInternalServerError,
Code: "Internal",
Details: &pb.ErrorDetails{ErrorId: ""},
expected: apiError{
status: http.StatusInternalServerError,
inner: &pb.Error{
Kind: "Internal",
Message: "Some random error",
},
},
},
{
name: "Db invalid public id error",
err: fmt.Errorf("test error: %w", errors.ErrInvalidPublicId),
expected: &pb.Error{
Status: http.StatusInternalServerError,
Code: "Internal",
Details: &pb.ErrorDetails{ErrorId: ""},
expected: apiError{
status: http.StatusInternalServerError,
inner: &pb.Error{
Kind: "Internal",
Message: fmt.Sprintf("test error: %s", errors.ErrInvalidPublicId),
},
},
},
{
name: "Db invalid parameter",
err: fmt.Errorf("test error: %w", errors.ErrInvalidParameter),
expected: &pb.Error{
Status: http.StatusInternalServerError,
Code: "Internal",
Details: &pb.ErrorDetails{ErrorId: ""},
expected: apiError{
status: http.StatusInternalServerError,
inner: &pb.Error{
Kind: "Internal",
Message: fmt.Sprintf("test error: %s", errors.ErrInvalidParameter),
},
},
},
{
name: "Db invalid field mask",
err: fmt.Errorf("test error: %w", errors.ErrInvalidFieldMask),
expected: &pb.Error{
Status: http.StatusBadRequest,
Code: "InvalidArgument",
Message: "Error in provided request",
Details: &pb.ErrorDetails{RequestFields: []*pb.FieldError{{Name: "update_mask", Description: "Invalid update mask provided."}}},
expected: apiError{
status: http.StatusBadRequest,
inner: &pb.Error{
Kind: "InvalidArgument",
Message: "Error in provided request",
Details: &pb.ErrorDetails{RequestFields: []*pb.FieldError{{Name: "update_mask", Description: "Invalid update mask provided."}}},
},
},
},
{
name: "Db empty field mask",
err: fmt.Errorf("test error: %w", errors.ErrEmptyFieldMask),
expected: &pb.Error{
Status: http.StatusBadRequest,
Code: "InvalidArgument",
Message: "Error in provided request",
Details: &pb.ErrorDetails{RequestFields: []*pb.FieldError{{Name: "update_mask", Description: "Invalid update mask provided."}}},
expected: apiError{
status: http.StatusBadRequest,
inner: &pb.Error{
Kind: "InvalidArgument",
Message: "Error in provided request",
Details: &pb.ErrorDetails{RequestFields: []*pb.FieldError{{Name: "update_mask", Description: "Invalid update mask provided."}}},
},
},
},
{
name: "Db not unique",
err: fmt.Errorf("test error: %w", errors.ErrNotUnique),
expected: &pb.Error{
Status: http.StatusBadRequest,
Code: "InvalidArgument",
Message: genericUniquenessMsg,
expected: apiError{
status: http.StatusBadRequest,
inner: &pb.Error{
Kind: "InvalidArgument",
Message: genericUniquenessMsg,
},
},
},
{
name: "Db record not found",
err: fmt.Errorf("test error: %w", errors.ErrRecordNotFound),
expected: &pb.Error{
Status: http.StatusNotFound,
Code: "NotFound",
Message: genericNotFoundMsg,
expected: apiError{
status: http.StatusNotFound,
inner: &pb.Error{
Kind: "NotFound",
Message: genericNotFoundMsg,
},
},
},
{
name: "Db multiple records",
err: fmt.Errorf("test error: %w", errors.ErrMultipleRecords),
expected: &pb.Error{
Status: http.StatusInternalServerError,
Code: "Internal",
Details: &pb.ErrorDetails{ErrorId: ""},
expected: apiError{
status: http.StatusInternalServerError,
inner: &pb.Error{
Kind: "Internal",
Message: fmt.Sprintf("test error: %s", errors.ErrMultipleRecords),
},
},
},
}
@@ -167,7 +191,7 @@ func TestApiErrorHandler(t *testing.T) {
w := httptest.NewRecorder()
tested(ctx, mux, outMarsh, w, req, tc.err)
resp := w.Result()
assert.EqualValues(tc.expected.Status, resp.StatusCode)
assert.EqualValues(tc.expected.status, resp.StatusCode)
got, err := ioutil.ReadAll(resp.Body)
require.NoError(err)
@@ -176,12 +200,8 @@ func TestApiErrorHandler(t *testing.T) {
err = inMarsh.Unmarshal(got, gotErr)
require.NoError(err)
if tc.expected.Status == http.StatusInternalServerError {
require.NotNil(tc.expected.GetDetails())
tc.expected.GetDetails().ErrorId = gotErr.GetDetails().GetErrorId()
}
assert.Empty(cmp.Diff(tc.expected, gotErr, protocmp.Transform()))
assert.Equal(tc.expected.status, int32(resp.StatusCode))
assert.Empty(cmp.Diff(tc.expected.inner, gotErr, protocmp.Transform()))
})
}
}