(w http.ResponseWriter, code int, payload any)
| 25 | } |
| 26 | |
| 27 | func RespondWithJSON(w http.ResponseWriter, code int, payload any) { |
| 28 | log := ctrllog.Log.WithName("http-helpers") |
| 29 | |
| 30 | response, err := json.Marshal(payload) |
| 31 | if err != nil { |
| 32 | log.Error(err, "Error marshalling JSON response") |
| 33 | RespondWithError(w, http.StatusInternalServerError, "Error marshalling JSON response") |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | w.Header().Set("Content-Type", "application/json") |
| 38 | w.WriteHeader(code) |
| 39 | w.Write(response) //nolint:errcheck |
| 40 | |
| 41 | log.V(2).Info("Sent JSON response", "statusCode", code, "responseSize", len(response)) |
| 42 | } |
| 43 | |
| 44 | func RespondWithError(w http.ResponseWriter, code int, message string) { |
| 45 | log := ctrllog.Log.WithName("http-helpers") |
no test coverage detected