fix: replace silent json.Encode with error-logging pattern across all handlers

This commit is contained in:
2026-07-11 17:50:07 +01:00
parent 0bef0f7973
commit 5d9fa1178b
24 changed files with 629 additions and 433 deletions
+20 -16
View File
@@ -67,10 +67,10 @@ func ToggleService(w http.ResponseWriter, r *http.Request) {
return
}
defer func() {
if err := tx.Rollback(r.Context()); err != nil && err.Error() != "tx is closed" {
slog.Error("failed to rollback transaction", "err", err)
}
}()
if err := tx.Rollback(r.Context()); err != nil && err.Error() != "tx is closed" {
slog.Error("failed to rollback transaction", "err", err)
}
}()
query := "UPDATE services SET is_active = NOT is_active WHERE id = $1"
result, err := tx.Exec(r.Context(), query, serviceID)
@@ -90,10 +90,12 @@ func ToggleService(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(map[string]any{
if err := json.NewEncoder(w).Encode(map[string]any{
"message": "Service toggled successfully",
"id": serviceID,
})
}); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
// POST /api/admin/services
@@ -145,10 +147,10 @@ func CreateServiceHandler(w http.ResponseWriter, r *http.Request) {
return
}
defer func() {
if err := tx.Rollback(r.Context()); err != nil && err.Error() != "tx is closed" {
slog.Error("failed to rollback transaction", "err", err)
}
}()
if err := tx.Rollback(r.Context()); err != nil && err.Error() != "tx is closed" {
slog.Error("failed to rollback transaction", "err", err)
}
}()
query := `
INSERT INTO services (
@@ -229,10 +231,10 @@ func DeleteServiceHandler(w http.ResponseWriter, r *http.Request) {
return
}
defer func() {
if err := tx.Rollback(r.Context()); err != nil && err.Error() != "tx is closed" {
slog.Error("failed to rollback transaction", "err", err)
}
}()
if err := tx.Rollback(r.Context()); err != nil && err.Error() != "tx is closed" {
slog.Error("failed to rollback transaction", "err", err)
}
}()
query := "UPDATE services SET is_active = FALSE WHERE id = $1"
result, err := tx.Exec(r.Context(), query, serviceID)
@@ -252,10 +254,12 @@ func DeleteServiceHandler(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(map[string]any{
if err := json.NewEncoder(w).Encode(map[string]any{
"message": "Service deleted successfully",
"id": serviceID,
})
}); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
// ServicesHandler returns all services from the database