[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:
parent
02d5ce7374
commit
505e3f9a89
|
@ -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.
|
||||||
|
```
|
|
@ -267,7 +267,7 @@ func (b *Backend) HandleRequest(ctx context.Context, req *logical.Request) (*log
|
||||||
if req.Operation != logical.HelpOperation {
|
if req.Operation != logical.HelpOperation {
|
||||||
err := fd.Validate()
|
err := fd.Validate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return logical.ErrorResponse(fmt.Sprintf("Field validation failed: %s", err.Error())), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,
|
Operation: logical.UpdateOperation,
|
||||||
Path: "foo/bar",
|
Path: "foo/bar",
|
||||||
Data: map[string]interface{}{"value": "3false3"},
|
Data: map[string]interface{}{"value": "3false3"},
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
if err == nil {
|
if !strings.Contains(resp.Data["error"].(string), "Field validation failed") {
|
||||||
t.Fatalf("should have thrown a conversion error")
|
t.Fatalf("bad: %#v", resp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue