Update to the error handling

This commit is contained in:
Aditya Ujeniya
2026-03-02 13:37:35 +01:00
parent 37a8706270
commit ef3504dcd8

View File

@@ -21,8 +21,7 @@ import (
cclog "github.com/ClusterCockpit/cc-lib/v2/ccLogger" cclog "github.com/ClusterCockpit/cc-lib/v2/ccLogger"
"github.com/ClusterCockpit/cc-lib/v2/schema" "github.com/ClusterCockpit/cc-lib/v2/schema"
"github.com/ClusterCockpit/cc-lib/v2/util" "github.com/ClusterCockpit/cc-lib/v2/util"
"github.com/ClusterCockpit/cc-line-protocol/v2/lineprotocol"
"github.com/influxdata/line-protocol/v2/lineprotocol"
) )
// ErrorResponse model // ErrorResponse model
@@ -37,6 +36,8 @@ type DefaultAPIResponse struct {
Message string `json:"msg"` Message string `json:"msg"`
} }
var ErrNoHostOrMetric error = errors.New("[METRICSTORE]> metric or host not found")
// handleError writes a standardized JSON error response with the given status code. // handleError writes a standardized JSON error response with the given status code.
// It logs the error at WARN level and ensures proper Content-Type headers are set. // It logs the error at WARN level and ensures proper Content-Type headers are set.
func handleError(err error, statusCode int, rw http.ResponseWriter) { func handleError(err error, statusCode int, rw http.ResponseWriter) {
@@ -249,15 +250,18 @@ func handleQuery(rw http.ResponseWriter, r *http.Request) {
res := make([]APIMetricData, 0, len(sels)) res := make([]APIMetricData, 0, len(sels))
for _, sel := range sels { for _, sel := range sels {
data := APIMetricData{} data := APIMetricData{}
if ver == "v1" {
data.Data, data.From, data.To, data.Resolution, err = ms.Read(sel, query.Metric, req.From, req.To, 0) data.Data, data.From, data.To, data.Resolution, err = ms.Read(sel, query.Metric, req.From, req.To, query.Resolution)
} else {
data.Data, data.From, data.To, data.Resolution, err = ms.Read(sel, query.Metric, req.From, req.To, query.Resolution)
}
if err != nil { if err != nil {
msg := err.Error() // Skip Error If Just Missing Host or Metric, Continue
data.Error = &msg // Empty Return For Metric Handled Gracefully By Frontend
res = append(res, data) if err != ErrNoHostOrMetric {
msg := err.Error()
data.Error = &msg
res = append(res, data)
} else {
cclog.Warnf("failed to fetch '%s' from host '%s' (cluster: %s): %s", query.Metric, query.Hostname, req.Cluster, err.Error())
}
continue continue
} }