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") w.Header().Set("Content-Type", "application/json")
snapshotPath, err := strg.CreateSnapshot() snapshotPath, err := strg.CreateSnapshot()
if err != nil { if err != nil {
msg := fmt.Sprintf("cannot create snapshot: %s", err) err = fmt.Errorf("cannot create snapshot: %s", err)
logger.Errorf("%s", msg) jsonResponseError(w, err)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, msg)
return true return true
} }
fmt.Fprintf(w, `{"status":"ok","snapshot":%q}`, snapshotPath) 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") w.Header().Set("Content-Type", "application/json")
snapshots, err := strg.ListSnapshots() snapshots, err := strg.ListSnapshots()
if err != nil { if err != nil {
msg := fmt.Sprintf("cannot list snapshots: %s", err) err = fmt.Errorf("cannot list snapshots: %s", err)
logger.Errorf("%s", msg) jsonResponseError(w, err)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, msg)
return true return true
} }
fmt.Fprintf(w, `{"status":"ok","snapshots":[`) 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") w.Header().Set("Content-Type", "application/json")
snapshotName := r.FormValue("snapshot") snapshotName := r.FormValue("snapshot")
if err := strg.DeleteSnapshot(snapshotName); err != nil { if err := strg.DeleteSnapshot(snapshotName); err != nil {
msg := fmt.Sprintf("cannot delete snapshot %q: %s", snapshotName, err) err = fmt.Errorf("cannot delete snapshot %q: %s", snapshotName, err)
logger.Errorf("%s", msg) jsonResponseError(w, err)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, msg)
return true return true
} }
fmt.Fprintf(w, `{"status":"ok"}`) 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") w.Header().Set("Content-Type", "application/json")
snapshots, err := strg.ListSnapshots() snapshots, err := strg.ListSnapshots()
if err != nil { if err != nil {
msg := fmt.Sprintf("cannot list snapshots: %s", err) err = fmt.Errorf("cannot list snapshots: %s", err)
logger.Errorf("%s", msg) jsonResponseError(w, err)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, msg)
return true return true
} }
for _, snapshotName := range snapshots { for _, snapshotName := range snapshots {
if err := strg.DeleteSnapshot(snapshotName); err != nil { if err := strg.DeleteSnapshot(snapshotName); err != nil {
msg := fmt.Sprintf("cannot delete snapshot %q: %s", snapshotName, err) err = fmt.Errorf("cannot delete snapshot %q: %s", snapshotName, err)
logger.Errorf("%s", msg) jsonResponseError(w, err)
fmt.Fprintf(w, `{"status":"error","msg":%q}`, msg)
return true return true
} }
} }
@ -517,3 +512,9 @@ func registerStorageMetrics(strg *storage.Storage) {
return float64(m().MetricNameCacheCollisions) 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)
}