app/vmstorage: return 500 status code instead of 200 status code on internal errors inside /snapshot/* handlers

This commit is contained in:
Aliaksandr Valialkin 2020-03-10 23:51:50 +02:00
parent b51e548b64
commit 8939c19281

View File

@ -116,9 +116,8 @@ func requestHandler(w http.ResponseWriter, r *http.Request, strg *storage.Storag
w.Header().Set("Content-Type", "application/json")
snapshotPath, err := strg.CreateSnapshot()
if err != nil {
msg := fmt.Sprintf("cannot create snapshot: %s", err)
logger.Errorf("%s", msg)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, msg)
err = fmt.Errorf("cannot create snapshot: %s", err)
jsonResponseError(w, err)
return true
}
fmt.Fprintf(w, `{"status":"ok","snapshot":%q}`, snapshotPath)
@ -127,9 +126,8 @@ func requestHandler(w http.ResponseWriter, r *http.Request, strg *storage.Storag
w.Header().Set("Content-Type", "application/json")
snapshots, err := strg.ListSnapshots()
if err != nil {
msg := fmt.Sprintf("cannot list snapshots: %s", err)
logger.Errorf("%s", msg)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, msg)
err = fmt.Errorf("cannot list snapshots: %s", err)
jsonResponseError(w, err)
return true
}
fmt.Fprintf(w, `{"status":"ok","snapshots":[`)
@ -145,9 +143,8 @@ func requestHandler(w http.ResponseWriter, r *http.Request, strg *storage.Storag
w.Header().Set("Content-Type", "application/json")
snapshotName := r.FormValue("snapshot")
if err := strg.DeleteSnapshot(snapshotName); err != nil {
msg := fmt.Sprintf("cannot delete snapshot %q: %s", snapshotName, err)
logger.Errorf("%s", msg)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, msg)
err = fmt.Errorf("cannot delete snapshot %q: %s", snapshotName, err)
jsonResponseError(w, err)
return true
}
fmt.Fprintf(w, `{"status":"ok"}`)
@ -156,16 +153,14 @@ func requestHandler(w http.ResponseWriter, r *http.Request, strg *storage.Storag
w.Header().Set("Content-Type", "application/json")
snapshots, err := strg.ListSnapshots()
if err != nil {
msg := fmt.Sprintf("cannot list snapshots: %s", err)
logger.Errorf("%s", msg)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, msg)
err = fmt.Errorf("cannot list snapshots: %s", err)
jsonResponseError(w, err)
return true
}
for _, snapshotName := range snapshots {
if err := strg.DeleteSnapshot(snapshotName); err != nil {
msg := fmt.Sprintf("cannot delete snapshot %q: %s", snapshotName, err)
logger.Errorf("%s", msg)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, msg)
err = fmt.Errorf("cannot delete snapshot %q: %s", snapshotName, err)
jsonResponseError(w, err)
return true
}
}
@ -517,3 +512,9 @@ func registerStorageMetrics(strg *storage.Storage) {
return float64(m().MetricNameCacheCollisions)
})
}
func jsonResponseError(w http.ResponseWriter, err error) {
logger.Errorf("%s", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, err)
}