From ec04cb8346e7ea5309963f43bf16622e13f94100 Mon Sep 17 00:00:00 2001 From: Sithum Sathsara <89584440+SithumSathsaras@users.noreply.github.com> Date: Fri, 18 Oct 2024 02:27:30 -0400 Subject: [PATCH] Fixed Issue: #2195 500 error when sending empty JSON payload to GraphQL server --- pkg/metrics/prometheus.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/metrics/prometheus.go b/pkg/metrics/prometheus.go index 7b1e939618..edc762a593 100644 --- a/pkg/metrics/prometheus.go +++ b/pkg/metrics/prometheus.go @@ -278,6 +278,7 @@ func (pc *prometheusCollector) MeasureGraphQLResponseDuration(next http.Handler) // Create a copy of the request body body, err := io.ReadAll(r.Body) + if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -292,6 +293,11 @@ func (pc *prometheusCollector) MeasureGraphQLResponseDuration(next http.Handler) var graphqlRequest struct { OperationName string `json:"operationName"` } + if !json.Valid(bodyCopy) { // Check if the body is valid JSON + err_msg := fmt.Sprintf("Provided input json couldn't be parsed. likely it was empty or was wrongly structured; error message: %v", err.Error()) + http.Error(w, err_msg, http.StatusBadRequest) + return + } if err := json.Unmarshal(bodyCopy, &graphqlRequest); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return