From 1d5a45655ebf82fb9b82022694e0d631ef602c45 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 20 Aug 2024 11:35:14 +0100 Subject: [PATCH] fix(service): add some error checking that was failing ci linting --- internal/adapter/http/port_handler.go | 10 ++++++++-- internal/adapter/http/ready.go | 6 +++++- 2 files changed, 13 insertions(+), 3 deletions(-) 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 + } }