diff --git a/internal/adapter/http/port_handler.go b/internal/adapter/http/port_handler.go index 4347398..eb59f75 100644 --- a/internal/adapter/http/port_handler.go +++ b/internal/adapter/http/port_handler.go @@ -2,8 +2,9 @@ package http import ( "encoding/json" - "github.com/willejs/ports-service/internal/controller" "net/http" + + "github.com/willejs/ports-service/internal/controller" ) // PortHandler handles HTTP requests for ports. @@ -25,5 +26,10 @@ func (h *PortHandler) ListPorts(w http.ResponseWriter, r *http.Request) { } w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(ports) + // Encode the ports to JSON and check for errors + if err := json.NewEncoder(w).Encode(ports); err != nil { + // return an error + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } } diff --git a/internal/adapter/http/ready.go b/internal/adapter/http/ready.go index 4806431..502e882 100644 --- a/internal/adapter/http/ready.go +++ b/internal/adapter/http/ready.go @@ -9,5 +9,9 @@ import ( func (h *PortHandler) Ready(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(map[string]bool{"ready": true}) + if err := json.NewEncoder(w).Encode(map[string]bool{"ready": true}); err != nil { + // Return an error + http.Error(w, "Failed to encode JSON response", http.StatusInternalServerError) + return + } }