prevent panic from empty POST payload

This commit is contained in:
Stephen McQuay 2013-12-21 01:27:33 -08:00
parent ae10b4087a
commit eacbcfa0a2
1 changed files with 8 additions and 0 deletions

8
api.go
View File

@ -56,6 +56,14 @@ func put(w http.ResponseWriter, req *http.Request) {
if *verbose {
log.Printf("incoming: '%s'", body)
}
// XXX: I am going to posit that there is something funky with how I write
// the error out below; If I don't check this, and I get to the
// JSONError{err.Error()} below then the app panics.
if string(body) == "" {
b, _ := json.Marshal(JSONError{"empty body"})
http.Error(w, string(b), http.StatusMethodNotAllowed)
return
}
parse_err := json.Unmarshal(body, &message)
if parse_err != nil {
b, _ := json.Marshal(JSONError{err.Error()})