forked from TrueCloudLab/certificates
api/render: use default error messages if none specified
This commit is contained in:
parent
c3cc60e211
commit
845e41967d
2 changed files with 11 additions and 0 deletions
|
@ -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…
Reference in a new issue