HTTP handlers often use this pattern:
func handler(w http.ResponseWriter, r *http.Request) {
data, err := fetchData()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(data)
}
Return early on error, respond normally on success. The http.Error function writes an error response.