From 05395032be864d0fbb187e5593c8a124844bb560 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Thu, 23 Apr 2020 16:23:21 +0800 Subject: [PATCH] config+server: Check for errors and handle them --- config.go | 4 ++-- server/api.go | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 4602a1a..6198e3c 100644 --- a/config.go +++ b/config.go @@ -16,6 +16,6 @@ func ReadConfig(file string) (Config, error) { return Config{}, err } var result Config - json.Unmarshal(bytes, &result) - return result, nil + err = json.Unmarshal(bytes, &result) + return result, err } diff --git a/server/api.go b/server/api.go index 270a186..f777cf2 100644 --- a/server/api.go +++ b/server/api.go @@ -11,7 +11,10 @@ func writeJSON(w http.ResponseWriter, a interface{}) { http.Error(w, "500 Internal Server Error", http.StatusInternalServerError) } w.Header().Set("Content-Type", "application/json") - w.Write(bytes) + _, err = w.Write(bytes) + if err != nil { + http.Error(w, "500 Internal Server Error", http.StatusInternalServerError) + } } func allStatus(w http.ResponseWriter, req *http.Request) {