mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-21 23:43:27 +01:00
Refactor handler errors
This commit is contained in:
parent
d69df41ef0
commit
d86afb2381
@ -36,8 +36,7 @@ func uptimeBadgeHandler(writer http.ResponseWriter, request *http.Request) {
|
|||||||
case "1h":
|
case "1h":
|
||||||
from = time.Now().Add(-time.Hour)
|
from = time.Now().Add(-time.Hour)
|
||||||
default:
|
default:
|
||||||
writer.WriteHeader(http.StatusBadRequest)
|
http.Error(writer, "Durations supported: 7d, 24h, 1h", http.StatusBadRequest)
|
||||||
_, _ = writer.Write([]byte("Durations supported: 7d, 24h, 1h"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
key := variables["key"]
|
key := variables["key"]
|
||||||
|
@ -39,8 +39,7 @@ func responseTimeChartHandler(writer http.ResponseWriter, r *http.Request) {
|
|||||||
case "24h":
|
case "24h":
|
||||||
from = time.Now().Truncate(time.Hour).Add(-24 * time.Hour)
|
from = time.Now().Truncate(time.Hour).Add(-24 * time.Hour)
|
||||||
default:
|
default:
|
||||||
writer.WriteHeader(http.StatusBadRequest)
|
http.Error(writer, "Durations supported: 7d, 24h", http.StatusBadRequest)
|
||||||
_, _ = writer.Write([]byte("Durations supported: 7d, 24h"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
hourlyAverageResponseTime, err := storage.Get().GetHourlyAverageResponseTimeByKey(vars["key"], from, time.Now())
|
hourlyAverageResponseTime, err := storage.Get().GetHourlyAverageResponseTimeByKey(vars["key"], from, time.Now())
|
||||||
|
@ -116,15 +116,13 @@ func serviceStatusesHandler(writer http.ResponseWriter, r *http.Request) {
|
|||||||
serviceStatuses, err := storage.Get().GetAllServiceStatuses(paging.NewServiceStatusParams().WithResults(page, pageSize))
|
serviceStatuses, err := storage.Get().GetAllServiceStatuses(paging.NewServiceStatusParams().WithResults(page, pageSize))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[controller][serviceStatusesHandler] Failed to retrieve service statuses: %s", err.Error())
|
log.Printf("[controller][serviceStatusesHandler] Failed to retrieve service statuses: %s", err.Error())
|
||||||
writer.WriteHeader(http.StatusInternalServerError)
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
||||||
_, _ = writer.Write([]byte(err.Error()))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data, err = json.Marshal(serviceStatuses)
|
data, err = json.Marshal(serviceStatuses)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[controller][serviceStatusesHandler] Unable to marshal object to JSON: %s", err.Error())
|
log.Printf("[controller][serviceStatusesHandler] Unable to marshal object to JSON: %s", err.Error())
|
||||||
writer.WriteHeader(http.StatusInternalServerError)
|
http.Error(writer, "unable to marshal object to JSON", http.StatusInternalServerError)
|
||||||
_, _ = writer.Write([]byte("Unable to marshal object to JSON"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, _ = gzipWriter.Write(data)
|
_, _ = 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))
|
serviceStatus, err := storage.Get().GetServiceStatusByKey(vars["key"], paging.NewServiceStatusParams().WithResults(page, pageSize).WithEvents(1, common.MaximumNumberOfEvents))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == common.ErrServiceNotFound {
|
if err == common.ErrServiceNotFound {
|
||||||
writer.WriteHeader(http.StatusNotFound)
|
http.Error(writer, err.Error(), http.StatusNotFound)
|
||||||
} else {
|
return
|
||||||
log.Printf("[controller][serviceStatusHandler] Failed to retrieve service status: %s", err.Error())
|
|
||||||
writer.WriteHeader(http.StatusInternalServerError)
|
|
||||||
}
|
}
|
||||||
_, _ = 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
|
return
|
||||||
}
|
}
|
||||||
if serviceStatus == nil {
|
if serviceStatus == nil {
|
||||||
log.Printf("[controller][serviceStatusHandler] Service with key=%s not found", vars["key"])
|
log.Printf("[controller][serviceStatusHandler] Service with key=%s not found", vars["key"])
|
||||||
writer.WriteHeader(http.StatusNotFound)
|
http.Error(writer, "not found", http.StatusNotFound)
|
||||||
_, _ = writer.Write([]byte("not found"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
output, err := json.Marshal(serviceStatus)
|
output, err := json.Marshal(serviceStatus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[controller][serviceStatusHandler] Unable to marshal object to JSON: %s", err.Error())
|
log.Printf("[controller][serviceStatusHandler] Unable to marshal object to JSON: %s", err.Error())
|
||||||
writer.WriteHeader(http.StatusInternalServerError)
|
http.Error(writer, "unable to marshal object to JSON", http.StatusInternalServerError)
|
||||||
_, _ = writer.Write([]byte("unable to marshal object to JSON"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
writer.Header().Add("Content-Type", "application/json")
|
writer.Header().Add("Content-Type", "application/json")
|
||||||
|
Loading…
Reference in New Issue
Block a user