Skip to content

Commit

Permalink
fix(service): add some error checking that was failing ci linting
Browse files Browse the repository at this point in the history
  • Loading branch information
willejs committed Aug 20, 2024
1 parent ddd5d0a commit 1d5a456
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 8 additions & 2 deletions internal/adapter/http/port_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
}
6 changes: 5 additions & 1 deletion internal/adapter/http/ready.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 1d5a456

Please sign in to comment.