api/render: use default error messages if none specified

pull/865/head
Panagiotis Siatras 2 years ago
parent c3cc60e211
commit 845e41967d
No known key found for this signature in database
GPG Key ID: 529695F03A572804

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

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

Loading…
Cancel
Save