fix: replace silent json.Encode with error-logging pattern across all handlers

This commit is contained in:
2026-07-11 17:50:07 +01:00
parent 0bef0f7973
commit 5d9fa1178b
24 changed files with 629 additions and 433 deletions
+7 -3
View File
@@ -2,6 +2,7 @@ package mw
import (
"encoding/json"
"log"
"net/http"
)
@@ -11,7 +12,9 @@ import (
func RespondJSON(w http.ResponseWriter, status int, data any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(data)
if err := json.NewEncoder(w).Encode(data); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
// RespondError sends a JSON error response with the given status code and message.
@@ -19,6 +22,7 @@ func RespondJSON(w http.ResponseWriter, status int, data any) {
func RespondError(w http.ResponseWriter, status int, msg string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]string{"error": msg})
if err := json.NewEncoder(w).Encode(map[string]string{"error": msg}); err != nil {
log.Printf("Failed to encode error response: %v", err)
}
}