Better error reporting

This commit is contained in:
Jakob Borg 2014-12-07 15:48:48 +01:00
parent 5dab0e50aa
commit 9c51cf50ad

30
main.go
View File

@ -33,8 +33,9 @@ var funcs = map[string]interface{}{
} }
func main() { func main() {
log.SetFlags(log.Lshortfile)
log.SetOutput(os.Stdout)
flag.Parse() flag.Parse()
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
fd, err := os.Open("static/index.html") fd, err := os.Open("static/index.html")
if err != nil { if err != nil {
@ -67,6 +68,8 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
log.Println("Listening on", listener.Addr())
srv := http.Server{ srv := http.Server{
ReadTimeout: 5 * time.Second, ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second, WriteTimeout: 5 * time.Second,
@ -120,11 +123,16 @@ func newDataHandler(w http.ResponseWriter, r *http.Request) {
idStr, ok := id.(string) idStr, ok := id.(string)
if !ok { if !ok {
if err != nil { if err != nil {
log.Println("No ID") log.Printf("No ID (type was %T)", id)
http.Error(w, "No ID", 500) http.Error(w, "No ID", 500)
return return
} }
} }
if idStr == "" {
log.Println("No ID (empty)")
http.Error(w, "No ID", 500)
return
}
// The ID is base64 encoded, so can contain slashes. Replace those with dots instead. // The ID is base64 encoded, so can contain slashes. Replace those with dots instead.
idStr = strings.Replace(idStr, "/", ".", -1) idStr = strings.Replace(idStr, "/", ".", -1)
@ -135,10 +143,22 @@ func newDataHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return
} }
json.NewEncoder(f).Encode(m) err = json.NewEncoder(f).Encode(m)
f.Close() if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
return
}
err = f.Close()
if err != nil {
log.Println(err)
http.Error(w, err.Error(), 500)
return
}
log.Printf("Report from %q", id)
} else { } else {
log.Println("No ID") log.Println("No ID (missing)")
http.Error(w, "No ID", 500) http.Error(w, "No ID", 500)
return return
} }