fix(demo): fail gracefully when /books-bought receives no POST data
This change makes the bookstore respond with an HTTP 400 error when the
request body is not valid JSON instead of crashing.
Fixes
@@ -88,7 +88,9 @@ func updateBooksSold(w http.ResponseWriter, r *http.Request) {
var updatedBooksSold int64
err := json.NewDecoder(r.Body).Decode(&updatedBooksSold)
if err != nil {
- log.Fatal().Err(err).Msg("Could not decode request body")
+ log.Error().Err(err).Msg("Could not decode request body")
+ w.WriteHeader(http.StatusBadRequest)
+ return
}
atomic.StoreInt64(&books.BooksSold, updatedBooksSold)
setHeaders(w, r)
