fixed some httpstatu.es

This commit is contained in:
Stephen McQuay 2013-11-15 08:54:52 -08:00
parent 50868809bd
commit f8fb523a76
1 changed files with 10 additions and 4 deletions

View File

@ -41,7 +41,6 @@ func startGame(w http.ResponseWriter, req *http.Request) {
}{} }{}
err = json.Unmarshal(body, &cfg) err = json.Unmarshal(body, &cfg)
if err != nil { if err != nil {
NewFailure(err.Error())
if err := json.NewEncoder(w).Encode(NewFailure(err.Error())); err != nil { if err := json.NewEncoder(w).Encode(NewFailure(err.Error())); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) 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) { 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) key, err := getGameId(req.URL.Path)
if err != nil { 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) log.Printf("requested stats for game: %s", key)
games.RLock() games.RLock()
g, ok := games.m[key] g, ok := games.m[key]
games.RUnlock() games.RUnlock()
if !ok { if !ok {
http.NotFound(w, req) b, _ := json.Marshal(NewFailure("game not found"))
http.Error(w, string(b), http.StatusNotFound)
return return
} }
g.winners.RLock() g.winners.RLock()
@ -130,7 +134,9 @@ func gameStats(w http.ResponseWriter, req *http.Request) {
func stopGame(w http.ResponseWriter, req *http.Request) { func stopGame(w http.ResponseWriter, req *http.Request) {
key, err := getGameId(req.URL.Path) key, err := getGameId(req.URL.Path)
if err != nil { 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() games.Lock()
g, ok := games.m[key] g, ok := games.m[key]