diff --git a/changelog/12042.txt b/changelog/12042.txt new file mode 100644 index 000000000..2ec23c834 --- /dev/null +++ b/changelog/12042.txt @@ -0,0 +1,3 @@ +```release-note:changes +api: A request that fails field validation will now be responded to with a 400 rather than 500. +``` diff --git a/sdk/framework/backend.go b/sdk/framework/backend.go index 2687045ce..a7a7f9adb 100644 --- a/sdk/framework/backend.go +++ b/sdk/framework/backend.go @@ -267,7 +267,7 @@ func (b *Backend) HandleRequest(ctx context.Context, req *logical.Request) (*log if req.Operation != logical.HelpOperation { err := fd.Validate() if err != nil { - return nil, err + return logical.ErrorResponse(fmt.Sprintf("Field validation failed: %s", err.Error())), nil } } diff --git a/sdk/framework/backend_test.go b/sdk/framework/backend_test.go index 9abd49d63..3d215080e 100644 --- a/sdk/framework/backend_test.go +++ b/sdk/framework/backend_test.go @@ -240,14 +240,17 @@ func TestBackendHandleRequest_badwrite(t *testing.T) { }, } - _, err := b.HandleRequest(context.Background(), &logical.Request{ + resp, err := b.HandleRequest(context.Background(), &logical.Request{ Operation: logical.UpdateOperation, Path: "foo/bar", Data: map[string]interface{}{"value": "3false3"}, }) + if err != nil { + t.Fatalf("err: %s", err) + } - if err == nil { - t.Fatalf("should have thrown a conversion error") + if !strings.Contains(resp.Data["error"].(string), "Field validation failed") { + t.Fatalf("bad: %#v", resp) } }