From d86afb2381dcff3ddb016e1a8e5cf38543566dae Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Sun, 12 Sep 2021 17:06:14 -0400 Subject: [PATCH] Refactor handler errors --- controller/badge.go | 3 +-- controller/chart.go | 3 +-- controller/controller.go | 21 ++++++++------------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/controller/badge.go b/controller/badge.go index e2e91e55..3c67ea06 100644 --- a/controller/badge.go +++ b/controller/badge.go @@ -36,8 +36,7 @@ func uptimeBadgeHandler(writer http.ResponseWriter, request *http.Request) { case "1h": from = time.Now().Add(-time.Hour) default: - writer.WriteHeader(http.StatusBadRequest) - _, _ = writer.Write([]byte("Durations supported: 7d, 24h, 1h")) + http.Error(writer, "Durations supported: 7d, 24h, 1h", http.StatusBadRequest) return } key := variables["key"] diff --git a/controller/chart.go b/controller/chart.go index 3a343648..bca979eb 100644 --- a/controller/chart.go +++ b/controller/chart.go @@ -39,8 +39,7 @@ func responseTimeChartHandler(writer http.ResponseWriter, r *http.Request) { case "24h": from = time.Now().Truncate(time.Hour).Add(-24 * time.Hour) default: - writer.WriteHeader(http.StatusBadRequest) - _, _ = writer.Write([]byte("Durations supported: 7d, 24h")) + http.Error(writer, "Durations supported: 7d, 24h", http.StatusBadRequest) return } hourlyAverageResponseTime, err := storage.Get().GetHourlyAverageResponseTimeByKey(vars["key"], from, time.Now()) diff --git a/controller/controller.go b/controller/controller.go index 97d7b25e..2b8eec8a 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -116,15 +116,13 @@ func serviceStatusesHandler(writer http.ResponseWriter, r *http.Request) { serviceStatuses, err := storage.Get().GetAllServiceStatuses(paging.NewServiceStatusParams().WithResults(page, pageSize)) if err != nil { log.Printf("[controller][serviceStatusesHandler] Failed to retrieve service statuses: %s", err.Error()) - writer.WriteHeader(http.StatusInternalServerError) - _, _ = writer.Write([]byte(err.Error())) + http.Error(writer, err.Error(), http.StatusInternalServerError) return } data, err = json.Marshal(serviceStatuses) if err != nil { log.Printf("[controller][serviceStatusesHandler] Unable to marshal object to JSON: %s", err.Error()) - writer.WriteHeader(http.StatusInternalServerError) - _, _ = writer.Write([]byte("Unable to marshal object to JSON")) + http.Error(writer, "unable to marshal object to JSON", http.StatusInternalServerError) return } _, _ = gzipWriter.Write(data) @@ -150,25 +148,22 @@ func serviceStatusHandler(writer http.ResponseWriter, r *http.Request) { serviceStatus, err := storage.Get().GetServiceStatusByKey(vars["key"], paging.NewServiceStatusParams().WithResults(page, pageSize).WithEvents(1, common.MaximumNumberOfEvents)) if err != nil { if err == common.ErrServiceNotFound { - writer.WriteHeader(http.StatusNotFound) - } else { - log.Printf("[controller][serviceStatusHandler] Failed to retrieve service status: %s", err.Error()) - writer.WriteHeader(http.StatusInternalServerError) + http.Error(writer, err.Error(), http.StatusNotFound) + return } - _, _ = writer.Write([]byte(err.Error())) + log.Printf("[controller][serviceStatusHandler] Failed to retrieve service status: %s", err.Error()) + http.Error(writer, err.Error(), http.StatusInternalServerError) return } if serviceStatus == nil { log.Printf("[controller][serviceStatusHandler] Service with key=%s not found", vars["key"]) - writer.WriteHeader(http.StatusNotFound) - _, _ = writer.Write([]byte("not found")) + http.Error(writer, "not found", http.StatusNotFound) return } output, err := json.Marshal(serviceStatus) if err != nil { log.Printf("[controller][serviceStatusHandler] Unable to marshal object to JSON: %s", err.Error()) - writer.WriteHeader(http.StatusInternalServerError) - _, _ = writer.Write([]byte("unable to marshal object to JSON")) + http.Error(writer, "unable to marshal object to JSON", http.StatusInternalServerError) return } writer.Header().Add("Content-Type", "application/json")