diff --git a/api/render/render.go b/api/render/render.go index 12ad70aa..5a37ec84 100644 --- a/api/render/render.go +++ b/api/render/render.go @@ -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"` diff --git a/api/render/render_test.go b/api/render/render_test.go index f1d7c145..8c7c99a4 100644 --- a/api/render/render_test.go +++ b/api/render/render_test.go @@ -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 {