Invalid Argument errors provide per field details. (#110)
* Invalid Argument errors now return a per field error message. * Creating an API error struct. * SDK uses field error details proto. * Adding tests specifically for handler helper error messages resulting in expected API output. * Inlining the error checks into the table driven tests. Use protocmp for assertions to make printing out diffs easier to parse. * Aggregate the invalid argument fields detected to be wrong. * Make the returned order of fields deterministic in the error details.. * Fixing formatting issue in errors_test.go. * Test to confirm unimplemented results in a 405 and not 404.
This commit is contained in:
1 parent
5333482ff8
commit
2c3fbe3024
10 files changed
+317
-186
No files matched your search
@@ -2,100 +2,113 @@ package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
pb "github.com/hashicorp/watchtower/internal/gen/controller/api"
|
||||
"github.com/stretchr/testify/assert"
|
||||
sdpb "google.golang.org/genproto/googleapis/rpc/errdetails"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/testing/protocmp"
|
||||
)
|
||||
|
||||
func TestApiErrorHandler(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
ctx := context.Background()
|
||||
req, err := http.NewRequest("GET", "madeup/for/the/test", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't create test request")
|
||||
}
|
||||
require.NoError(err)
|
||||
mux := runtime.NewServeMux()
|
||||
_, outMarsh := runtime.MarshalerForRequest(mux, req)
|
||||
inMarsh, outMarsh := runtime.MarshalerForRequest(mux, req)
|
||||
|
||||
tested := ErrorHandler(hclog.L())
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
err error
|
||||
statusDetails []proto.Message
|
||||
expected *pb.Error
|
||||
name string
|
||||
err error
|
||||
expected *pb.Error
|
||||
}{
|
||||
{
|
||||
name: "Not Found",
|
||||
err: status.Error(codes.NotFound, "test"),
|
||||
err: NotFoundErrorf("Test"),
|
||||
expected: &pb.Error{
|
||||
Status: 404,
|
||||
Code: codes.NotFound.String(),
|
||||
Message: "test",
|
||||
Status: http.StatusNotFound,
|
||||
Code: "NotFound",
|
||||
Message: "Test",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Invalid Fields",
|
||||
err: InvalidArgumentErrorf("Test", map[string]string{
|
||||
"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",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "GrpcGateway Routing Error",
|
||||
err: runtime.ErrUnknownURI,
|
||||
expected: &pb.Error{
|
||||
Status: 404,
|
||||
Code: codes.NotFound.String(),
|
||||
Status: http.StatusNotFound,
|
||||
Code: "NotFound",
|
||||
Message: http.StatusText(http.StatusNotFound),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Invalid Fields",
|
||||
err: status.Error(codes.InvalidArgument, "test"),
|
||||
statusDetails: []proto.Message{
|
||||
&sdpb.BadRequest{
|
||||
FieldViolations: []*sdpb.BadRequest_FieldViolation{
|
||||
{Field: "first"},
|
||||
{Field: "second"},
|
||||
},
|
||||
},
|
||||
},
|
||||
name: "Unimplemented error",
|
||||
err: status.Error(codes.Unimplemented, "Test"),
|
||||
expected: &pb.Error{
|
||||
Status: 400,
|
||||
Code: codes.InvalidArgument.String(),
|
||||
Message: "test",
|
||||
Details: &pb.ErrorDetails{
|
||||
RequestFields: []string{"first", "second"},
|
||||
},
|
||||
Status: http.StatusMethodNotAllowed,
|
||||
Code: "Unimplemented",
|
||||
Message: "Test",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Unknown error",
|
||||
err: errors.New("Some random error"),
|
||||
expected: &pb.Error{
|
||||
Status: http.StatusInternalServerError,
|
||||
Code: "Unknown",
|
||||
Message: "Some random error",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
if tc.statusDetails != nil {
|
||||
s, ok := status.FromError(tc.err)
|
||||
assert.True(ok)
|
||||
s, err := s.WithDetails(tc.statusDetails...)
|
||||
assert.NoError(err)
|
||||
tc.err = s.Err()
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
tested(ctx, mux, outMarsh, w, req, tc.err)
|
||||
resp := w.Result()
|
||||
assert.EqualValues(tc.expected.Status, resp.StatusCode)
|
||||
|
||||
got, err := ioutil.ReadAll(resp.Body)
|
||||
assert.NoError(err)
|
||||
want, err := outMarsh.Marshal(tc.expected)
|
||||
t.Logf("Got marshaled error: %q", want)
|
||||
assert.NoError(err)
|
||||
assert.JSONEq(string(want), string(got))
|
||||
require.NoError(err)
|
||||
|
||||
gotErr := &pb.Error{}
|
||||
err = inMarsh.Unmarshal(got, gotErr)
|
||||
require.NoError(err)
|
||||
assert.Empty(cmp.Diff(tc.expected, gotErr, protocmp.Transform()))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user