From 5e711fb3b9d9834949a8771fcf9b7a42ab6c6ccc Mon Sep 17 00:00:00 2001 From: TwiN Date: Mon, 8 Nov 2021 20:56:35 -0500 Subject: [PATCH] Use http.Error instead of writer.Write --- controller/handler/badge.go | 14 ++++++-------- controller/handler/chart.go | 10 ++++------ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/controller/handler/badge.go b/controller/handler/badge.go index 3b7ec6b4..99e1cc24 100644 --- a/controller/handler/badge.go +++ b/controller/handler/badge.go @@ -43,13 +43,12 @@ func UptimeBadge(writer http.ResponseWriter, request *http.Request) { uptime, err := store.Get().GetUptimeByKey(key, from, time.Now()) if err != nil { if err == common.ErrEndpointNotFound { - writer.WriteHeader(http.StatusNotFound) + http.Error(writer, err.Error(), http.StatusNotFound) } else if err == common.ErrInvalidTimeRange { - writer.WriteHeader(http.StatusBadRequest) + http.Error(writer, err.Error(), http.StatusBadRequest) } else { - writer.WriteHeader(http.StatusInternalServerError) + http.Error(writer, err.Error(), http.StatusInternalServerError) } - _, _ = writer.Write([]byte(err.Error())) return } formattedDate := time.Now().Format(http.TimeFormat) @@ -82,13 +81,12 @@ func ResponseTimeBadge(writer http.ResponseWriter, request *http.Request) { averageResponseTime, err := store.Get().GetAverageResponseTimeByKey(key, from, time.Now()) if err != nil { if err == common.ErrEndpointNotFound { - writer.WriteHeader(http.StatusNotFound) + http.Error(writer, err.Error(), http.StatusNotFound) } else if err == common.ErrInvalidTimeRange { - writer.WriteHeader(http.StatusBadRequest) + http.Error(writer, err.Error(), http.StatusBadRequest) } else { - writer.WriteHeader(http.StatusInternalServerError) + http.Error(writer, err.Error(), http.StatusInternalServerError) } - _, _ = writer.Write([]byte(err.Error())) return } formattedDate := time.Now().Format(http.TimeFormat) diff --git a/controller/handler/chart.go b/controller/handler/chart.go index f945501a..b8213b9c 100644 --- a/controller/handler/chart.go +++ b/controller/handler/chart.go @@ -45,18 +45,16 @@ func ResponseTimeChart(writer http.ResponseWriter, r *http.Request) { hourlyAverageResponseTime, err := store.Get().GetHourlyAverageResponseTimeByKey(vars["key"], from, time.Now()) if err != nil { if err == common.ErrEndpointNotFound { - writer.WriteHeader(http.StatusNotFound) + http.Error(writer, err.Error(), http.StatusNotFound) } else if err == common.ErrInvalidTimeRange { - writer.WriteHeader(http.StatusBadRequest) + http.Error(writer, err.Error(), http.StatusBadRequest) } else { - writer.WriteHeader(http.StatusInternalServerError) + http.Error(writer, err.Error(), http.StatusInternalServerError) } - _, _ = writer.Write([]byte(err.Error())) return } if len(hourlyAverageResponseTime) == 0 { - writer.WriteHeader(http.StatusNoContent) - _, _ = writer.Write(nil) + http.Error(writer, "", http.StatusNoContent) return } series := chart.TimeSeries{