From 22b15cada4ede18bf3b9e3634418c13105a85b74 Mon Sep 17 00:00:00 2001 From: Steve Howes Date: Wed, 10 Apr 2024 22:10:20 +0100 Subject: [PATCH] Tweak error handling --- dorset-binformation.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/dorset-binformation.go b/dorset-binformation.go index db4c0c3..1785e6e 100644 --- a/dorset-binformation.go +++ b/dorset-binformation.go @@ -5,7 +5,7 @@ import ( "errors" "flag" "fmt" - "io/ioutil" + "io" "log" "net/http" "os" @@ -22,7 +22,7 @@ func getHTML(url string) (*html.Node, error) { } defer resp.Body.Close() - b, err := ioutil.ReadAll(resp.Body) + b, err := io.ReadAll(resp.Body) if err != nil { return nil, err } @@ -96,6 +96,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request, logger *log.Logger) { // If we got an error from the council (in text) pass it on err = findDates(doc, dates) + if err != nil { logger.Println("Error retrieving data: ", err) w.WriteHeader(http.StatusInternalServerError) @@ -105,11 +106,16 @@ func handleRequest(w http.ResponseWriter, r *http.Request, logger *log.Logger) { jsonData, err := json.Marshal(dates) if err != nil { logger.Println(err) + w.WriteHeader(http.StatusInternalServerError) + return } // return jsonData to the client w.Header().Set("Content-Type", "application/json") - w.Write(jsonData) + _, err = w.Write(jsonData) + if err != nil { + logger.Println("Error writing response: ", err) + } } func main() {