package web import ( "encoding/json" "log" "net/http" ) type errorResponse struct { Error string `json:"error"` } func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { data, err := json.Marshal(payload) if err != nil { log.Printf("failed to marshal JSON response: %v\n", err) w.WriteHeader(http.StatusInternalServerError) return } w.Header().Add("Content-Type", "application/json") w.WriteHeader(code) _, err = w.Write(data) if err != nil { log.Printf("failed to write data to output buffer: %v\n", err) } } func respondWithError(w http.ResponseWriter, code int, message string) { if code >= http.StatusInternalServerError { log.Println("responding with 5xx error:", message) } respondWithJSON(w, code, errorResponse{Error: message}) }