Răsfoiți Sursa

Convert all error messages back to 500 wrapper

Niko Kovacevic 5 ani în urmă
părinte
comite
12eda3f8cc
2 a modificat fișierele cu 10 adăugiri și 10 ștergeri
  1. 7 7
      pkg/costmodel/aggregation.go
  2. 3 3
      pkg/costmodel/router.go

+ 7 - 7
pkg/costmodel/aggregation.go

@@ -1898,19 +1898,19 @@ func (a *Accesses) AggregateCostModelHandler(w http.ResponseWriter, r *http.Requ
 
 
 	// aggregation field is required
 	// aggregation field is required
 	if field == "" {
 	if field == "" {
-		http.Error(w, "Missing aggregation field parameter", http.StatusBadRequest)
+		w.Write(WrapDataWithMessage(nil, fmt.Errorf("Missing aggregation field parameter"), "Missing aggregation field parameter"))
 		return
 		return
 	}
 	}
 
 
 	// aggregation subfield is required when aggregation field is "label"
 	// aggregation subfield is required when aggregation field is "label"
 	if field == "label" && len(subfields) == 0 {
 	if field == "label" && len(subfields) == 0 {
-		http.Error(w, "Missing aggregation subfield parameter for aggregation by label", http.StatusBadRequest)
+		w.Write(WrapDataWithMessage(nil, fmt.Errorf("Missing aggregation subfield parameter for aggregation by label"), "Missing aggregation subfield parameter for aggregation by label"))
 		return
 		return
 	}
 	}
 
 
 	// enforce one of four available rate options
 	// enforce one of four available rate options
 	if opts.Rate != "" && opts.Rate != "hourly" && opts.Rate != "daily" && opts.Rate != "monthly" {
 	if opts.Rate != "" && opts.Rate != "hourly" && opts.Rate != "daily" && opts.Rate != "monthly" {
-		http.Error(w, "If set, rate parameter must be one of: 'hourly', 'daily', 'monthly'", http.StatusBadRequest)
+		w.Write(WrapDataWithMessage(nil, fmt.Errorf("If set, rate parameter must be one of: 'hourly', 'daily', 'monthly'"), "If set, rate parameter must be one of: 'hourly', 'daily', 'monthly'"))
 		return
 		return
 	}
 	}
 
 
@@ -1936,7 +1936,7 @@ func (a *Accesses) AggregateCostModelHandler(w http.ResponseWriter, r *http.Requ
 		sln = strings.Split(sharedLabelNames, ",")
 		sln = strings.Split(sharedLabelNames, ",")
 		slv = strings.Split(sharedLabelValues, ",")
 		slv = strings.Split(sharedLabelValues, ",")
 		if len(sln) != len(slv) || slv[0] == "" {
 		if len(sln) != len(slv) || slv[0] == "" {
-			http.Error(w, "Supply exacly one shared label value per shared label name", http.StatusBadRequest)
+			w.Write(WrapDataWithMessage(nil, fmt.Errorf("Supply exacly one shared label value per shared label name"), "Supply exacly one shared label value per shared label name"))
 			return
 			return
 		}
 		}
 	}
 	}
@@ -1980,15 +1980,15 @@ func (a *Accesses) AggregateCostModelHandler(w http.ResponseWriter, r *http.Requ
 					}
 					}
 				}
 				}
 
 
-				http.Error(w, msg, http.StatusInternalServerError)
+				w.Write(WrapDataWithMessage(nil, fmt.Errorf(msg), msg))
 			} else {
 			} else {
 				// Boundary error outside of 90 day period; may not be available
 				// Boundary error outside of 90 day period; may not be available
-				http.Error(w, boundaryErr.Error(), http.StatusInternalServerError)
+				w.Write(WrapDataWithMessage(nil, boundaryErr, boundaryErr.Error()))
 			}
 			}
 			return
 			return
 		}
 		}
 		errStr := fmt.Sprintf("error computing aggregate cost model: %s", err)
 		errStr := fmt.Sprintf("error computing aggregate cost model: %s", err)
-		http.Error(w, errStr, http.StatusInternalServerError)
+		w.Write(WrapDataWithMessage(nil, fmt.Errorf(errStr), errStr))
 		return
 		return
 	}
 	}
 
 

+ 3 - 3
pkg/costmodel/router.go

@@ -484,18 +484,18 @@ func (a *Accesses) CostDataModelRange(w http.ResponseWriter, r *http.Request, ps
 	layout := "2006-01-02T15:04:05.000Z"
 	layout := "2006-01-02T15:04:05.000Z"
 	start, err := time.Parse(layout, startStr)
 	start, err := time.Parse(layout, startStr)
 	if err != nil {
 	if err != nil {
-		http.Error(w, fmt.Sprintf("invalid start date: %s", startStr), http.StatusBadRequest)
+		w.Write(WrapDataWithMessage(nil, fmt.Errorf("invalid start date: %s", startStr), fmt.Sprintf("invalid start date: %s", startStr)))
 		return
 		return
 	}
 	}
 	end, err := time.Parse(layout, endStr)
 	end, err := time.Parse(layout, endStr)
 	if err != nil {
 	if err != nil {
-		http.Error(w, fmt.Sprintf("invalid end date: %s", endStr), http.StatusBadRequest)
+		w.Write(WrapDataWithMessage(nil, fmt.Errorf("invalid end date: %s", endStr), fmt.Sprintf("invalid end date: %s", endStr)))
 		return
 		return
 	}
 	}
 
 
 	window := kubecost.NewWindow(&start, &end)
 	window := kubecost.NewWindow(&start, &end)
 	if window.IsOpen() || window.IsEmpty() || window.IsNegative() {
 	if window.IsOpen() || window.IsEmpty() || window.IsNegative() {
-		http.Error(w, fmt.Sprintf("invalid date range: %s", window), http.StatusBadRequest)
+		w.Write(WrapDataWithMessage(nil, fmt.Errorf("invalid date range: %s", window), fmt.Sprintf("invalid date range: %s", window)))
 		return
 		return
 	}
 	}