Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ import (
func WriteJSONResponse(w http.ResponseWriter, statusCode int, body interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(body)
data, err := json.Marshal(body)
if err != nil {
return
Comment on lines 13 to +17
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json.Marshal is done after WriteHeader(statusCode). If marshaling fails, this returns after headers/status have already been sent, leaving an empty body with the original status code (often 200) and no indication of the server error. Consider marshaling first, and on error writing an explicit 500 (or at least the correct error status) before sending headers; also consider handling/logging the w.Write error.

See below for a potential fix:

	data, err := json.Marshal(body)
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(statusCode)
	if _, err := w.Write(data); err != nil {
		return
	}

Copilot uses AI. Check for mistakes.
}
w.Write(data)
}
Loading