Skip to content

Commit

Permalink
Fixes errors from control service
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Jan 31, 2025
1 parent 143739d commit 4cd34f0
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions services/service_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *ControlService) handleIsPaused(w http.ResponseWriter, r *http.Request)
name := r.URL.Query().Get("name")
results, err := s.manager.IsPaused(name)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
writeJSONErrorResponse(w, err.Error(), http.StatusBadRequest)
return
}
writeJSONResponse(w, results)
Expand All @@ -99,8 +99,9 @@ func (s *ControlService) handleIsPaused(w http.ResponseWriter, r *http.Request)
func (s *ControlService) handlePause(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
results, err := s.manager.Pause(name)
s.logger.Info("Pausing service", "name", name, "results", results, "err", err)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
writeJSONErrorResponse(w, err.Error(), http.StatusBadRequest)
return
}
writeJSONResponse(w, results)
Expand All @@ -110,7 +111,7 @@ func (s *ControlService) handleUnpause(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
results, err := s.manager.Unpause(name)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
writeJSONErrorResponse(w, err.Error(), http.StatusBadRequest)
return
}
writeJSONResponse(w, results)
Expand All @@ -120,3 +121,9 @@ func writeJSONResponse(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}

func writeJSONErrorResponse(w http.ResponseWriter, message string, status int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(map[string]string{"error": message})
}

0 comments on commit 4cd34f0

Please sign in to comment.