api/render: use default error messages if none specified

This commit is contained in:
Panagiotis Siatras 2022-03-22 19:15:23 +02:00
parent c3cc60e211
commit 845e41967d
No known key found for this signature in database
GPG key ID: 529695F03A572804
2 changed files with 11 additions and 0 deletions

View file

@ -116,11 +116,17 @@ func Error(w http.ResponseWriter, err error) {
// BadRequest renders the JSON representation of err into w and sets its
// status code to http.StatusBadRequest.
//
// In case err is nil, a default error message will be used in its place.
func BadRequest(w http.ResponseWriter, err error) {
codedError(w, http.StatusBadRequest, err)
}
func codedError(w http.ResponseWriter, code int, err error) {
if err == nil {
err = errors.New(http.StatusText(code))
}
var wrapper = struct {
Status int `json:"status"`
Message string `json:"message"`

View file

@ -69,6 +69,11 @@ func TestErrors(t *testing.T) {
code: http.StatusBadRequest,
body: `{"status":400,"message":"assert.AnError general error for testing"}`,
},
1: {
fn: BadRequest,
code: http.StatusBadRequest,
body: `{"status":400,"message":"Bad Request"}`,
},
}
for caseIndex := range cases {