From f8fb523a7628bb32ba5ffea76e5f1fb6e475f691 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Fri, 15 Nov 2013 08:54:52 -0800 Subject: [PATCH] fixed some httpstatu.es --- control.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/control.go b/control.go index 9c1befe..e0f4315 100644 --- a/control.go +++ b/control.go @@ -41,7 +41,6 @@ func startGame(w http.ResponseWriter, req *http.Request) { }{} err = json.Unmarshal(body, &cfg) if err != nil { - NewFailure(err.Error()) if err := json.NewEncoder(w).Encode(NewFailure(err.Error())); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } @@ -108,16 +107,21 @@ func listGames(w http.ResponseWriter, req *http.Request) { } func gameStats(w http.ResponseWriter, req *http.Request) { + // TODO: wrap this up in something similar to the JsonHandler to verify the + // url? Look at gorilla routing? key, err := getGameId(req.URL.Path) if err != nil { - http.Error(w, "improperly formed url", http.StatusBadRequest) + b, _ := json.Marshal(NewFailure(err.Error())) + http.Error(w, string(b), http.StatusBadRequest) + return } log.Printf("requested stats for game: %s", key) games.RLock() g, ok := games.m[key] games.RUnlock() if !ok { - http.NotFound(w, req) + b, _ := json.Marshal(NewFailure("game not found")) + http.Error(w, string(b), http.StatusNotFound) return } g.winners.RLock() @@ -130,7 +134,9 @@ func gameStats(w http.ResponseWriter, req *http.Request) { func stopGame(w http.ResponseWriter, req *http.Request) { key, err := getGameId(req.URL.Path) if err != nil { - http.Error(w, "improperly formed url", http.StatusBadRequest) + b, _ := json.Marshal(NewFailure(err.Error())) + http.Error(w, string(b), http.StatusBadRequest) + return } games.Lock() g, ok := games.m[key]