Use http.Error instead of writer.Write

This commit is contained in:
TwiN 2021-11-08 20:56:35 -05:00
parent ab66e7ec8a
commit 5e711fb3b9
2 changed files with 10 additions and 14 deletions

View File

@ -43,13 +43,12 @@ func UptimeBadge(writer http.ResponseWriter, request *http.Request) {
uptime, err := store.Get().GetUptimeByKey(key, from, time.Now()) uptime, err := store.Get().GetUptimeByKey(key, from, time.Now())
if err != nil { if err != nil {
if err == common.ErrEndpointNotFound { if err == common.ErrEndpointNotFound {
writer.WriteHeader(http.StatusNotFound) http.Error(writer, err.Error(), http.StatusNotFound)
} else if err == common.ErrInvalidTimeRange { } else if err == common.ErrInvalidTimeRange {
writer.WriteHeader(http.StatusBadRequest) http.Error(writer, err.Error(), http.StatusBadRequest)
} else { } else {
writer.WriteHeader(http.StatusInternalServerError) http.Error(writer, err.Error(), http.StatusInternalServerError)
} }
_, _ = writer.Write([]byte(err.Error()))
return return
} }
formattedDate := time.Now().Format(http.TimeFormat) 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()) averageResponseTime, err := store.Get().GetAverageResponseTimeByKey(key, from, time.Now())
if err != nil { if err != nil {
if err == common.ErrEndpointNotFound { if err == common.ErrEndpointNotFound {
writer.WriteHeader(http.StatusNotFound) http.Error(writer, err.Error(), http.StatusNotFound)
} else if err == common.ErrInvalidTimeRange { } else if err == common.ErrInvalidTimeRange {
writer.WriteHeader(http.StatusBadRequest) http.Error(writer, err.Error(), http.StatusBadRequest)
} else { } else {
writer.WriteHeader(http.StatusInternalServerError) http.Error(writer, err.Error(), http.StatusInternalServerError)
} }
_, _ = writer.Write([]byte(err.Error()))
return return
} }
formattedDate := time.Now().Format(http.TimeFormat) formattedDate := time.Now().Format(http.TimeFormat)

View File

@ -45,18 +45,16 @@ func ResponseTimeChart(writer http.ResponseWriter, r *http.Request) {
hourlyAverageResponseTime, err := store.Get().GetHourlyAverageResponseTimeByKey(vars["key"], from, time.Now()) hourlyAverageResponseTime, err := store.Get().GetHourlyAverageResponseTimeByKey(vars["key"], from, time.Now())
if err != nil { if err != nil {
if err == common.ErrEndpointNotFound { if err == common.ErrEndpointNotFound {
writer.WriteHeader(http.StatusNotFound) http.Error(writer, err.Error(), http.StatusNotFound)
} else if err == common.ErrInvalidTimeRange { } else if err == common.ErrInvalidTimeRange {
writer.WriteHeader(http.StatusBadRequest) http.Error(writer, err.Error(), http.StatusBadRequest)
} else { } else {
writer.WriteHeader(http.StatusInternalServerError) http.Error(writer, err.Error(), http.StatusInternalServerError)
} }
_, _ = writer.Write([]byte(err.Error()))
return return
} }
if len(hourlyAverageResponseTime) == 0 { if len(hourlyAverageResponseTime) == 0 {
writer.WriteHeader(http.StatusNoContent) http.Error(writer, "", http.StatusNoContent)
_, _ = writer.Write(nil)
return return
} }
series := chart.TimeSeries{ series := chart.TimeSeries{