[VAULT-2825] Fix erroneous 500 resp for field validation errors (#12042)

* [VAULT-2825] Correctly respond with 400 rather than 500 for field validation errors

* [VAULT-2825] Add changelog entry

* [VAULT-2825] Simplify test assertion
This commit is contained in:
Chris Capurso 2021-07-12 13:39:28 -04:00 committed by GitHub
parent 02d5ce7374
commit 505e3f9a89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 4 deletions

3
changelog/12042.txt Normal file
View File

@ -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.
```

View File

@ -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
}
}

View File

@ -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)
}
}