fix: replace silent json.Encode with error-logging pattern across all handlers
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user