Skip to content

Commit 3bbf3d2

Browse files
committed
bad request response
1 parent d89b78f commit 3bbf3d2

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

go/samples/http/todos/controller.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (c *TodosController) ActionInsert(w http.ResponseWriter, r *http.Request, p
6464
var dto TodoDTO
6565
err := json.NewDecoder(r.Body).Decode(&dto)
6666
if err != nil {
67-
writeServerErrorResponse(w, "parsing body failed")
67+
writeBadRequestResponse(w, "parsing body failed")
6868
return
6969
}
7070

@@ -144,7 +144,7 @@ func (c *TodosController) ActionUpdate(w http.ResponseWriter, r *http.Request, p
144144
var dto TodoDTO
145145
err := json.NewDecoder(r.Body).Decode(&dto)
146146
if err != nil {
147-
writeServerErrorResponse(w, "parsing body failed")
147+
writeBadRequestResponse(w, "parsing body failed")
148148
return
149149
}
150150

@@ -254,3 +254,20 @@ func writeNotFoundResponse(w http.ResponseWriter) {
254254
w.Write(resJson)
255255
return
256256
}
257+
258+
func writeBadRequestResponse(w http.ResponseWriter, reason string) {
259+
w.WriteHeader(http.StatusBadRequest)
260+
w.Header().Set("Content-Type", "application/json")
261+
262+
res := make(map[string]string)
263+
res["msg"] = "bad request"
264+
res["reason"] = reason
265+
266+
resJson, err := json.Marshal(res)
267+
if err != nil {
268+
log.Fatalf("error: json marshall: %v", res)
269+
}
270+
271+
w.Write(resJson)
272+
return
273+
}

0 commit comments

Comments
 (0)