Skip to content

Commit

Permalink
improve error message returned by metrics handler
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-malachyn committed Jul 23, 2024
1 parent 99c2d93 commit 178d6df
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions metrics/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metrics

import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
Expand Down Expand Up @@ -36,15 +37,17 @@ func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func extractMethod(w http.ResponseWriter, r *http.Request) (string, error) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusBadRequest)
errMsg := fmt.Sprintf("error reading request body: %s", err)
http.Error(w, errMsg, http.StatusBadRequest)
return "", err
}

var requestBody struct {
Method string `json:"method"`
}
if err := json.Unmarshal(body, &requestBody); err != nil {
http.Error(w, "Error extracting method field from body", http.StatusBadRequest)
errMsg := fmt.Sprintf("error extracting method from body: %s", err)
http.Error(w, errMsg, http.StatusBadRequest)
return "", err
}

Expand Down

0 comments on commit 178d6df

Please sign in to comment.