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
+35 -21
View File
@@ -105,7 +105,9 @@ func GetCustomServices(w http.ResponseWriter, r *http.Request) {
if services == nil {
services = []CustomService{}
}
_ = json.NewEncoder(w).Encode(services)
if err := json.NewEncoder(w).Encode(services); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
return
}
@@ -192,12 +194,14 @@ func GetCustomServices(w http.ResponseWriter, r *http.Request) {
if services == nil {
services = []CustomService{}
}
_ = json.NewEncoder(w).Encode(CustomServiceListResponse{
if err := json.NewEncoder(w).Encode(CustomServiceListResponse{
Services: services,
Total: total,
PerPage: perPage,
NextCursor: nextCursor,
})
}); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
func CreateCustomService(w http.ResponseWriter, r *http.Request) {
@@ -266,7 +270,9 @@ func CreateCustomService(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(cs)
if err := json.NewEncoder(w).Encode(cs); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
func GetCustomService(w http.ResponseWriter, r *http.Request) {
@@ -306,7 +312,9 @@ func GetCustomService(w http.ResponseWriter, r *http.Request) {
cs.LastUsedAt = &lastUsedAt.Time
}
_ = json.NewEncoder(w).Encode(cs)
if err := json.NewEncoder(w).Encode(cs); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
func UpdateCustomService(w http.ResponseWriter, r *http.Request) {
@@ -387,10 +395,10 @@ func UpdateCustomService(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)
}
}()
result, err := tx.Exec(r.Context(), query, args...)
if err != nil {
@@ -407,7 +415,9 @@ func UpdateCustomService(w http.ResponseWriter, r *http.Request) {
return
}
_ = json.NewEncoder(w).Encode(map[string]string{"message": "Custom service updated"})
if err := json.NewEncoder(w).Encode(map[string]string{"message": "Custom service updated"}); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
func PromoteCustomService(w http.ResponseWriter, r *http.Request) {
@@ -423,10 +433,10 @@ func PromoteCustomService(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)
}
}()
var name, desc, notes sql.NullString
var price float64
@@ -496,11 +506,13 @@ func PromoteCustomService(w http.ResponseWriter, r *http.Request) {
return
}
_ = json.NewEncoder(w).Encode(map[string]string{
if err := json.NewEncoder(w).Encode(map[string]string{
"message": "Custom service promoted to regular service",
"new_service_id": newServiceID,
"custom_service_id": id,
})
}); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
func DeleteCustomService(w http.ResponseWriter, r *http.Request) {
@@ -531,10 +543,10 @@ func DeleteCustomService(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)
}
}()
result, err := tx.Exec(r.Context(), `DELETE FROM custom_services WHERE id = $1`, id)
if err != nil {
@@ -551,7 +563,9 @@ func DeleteCustomService(w http.ResponseWriter, r *http.Request) {
return
}
_ = json.NewEncoder(w).Encode(map[string]string{"message": "Custom service deleted"})
if err := json.NewEncoder(w).Encode(map[string]string{"message": "Custom service deleted"}); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
func joinStrings(strs []string, sep string) string {
+16 -14
View File
@@ -282,10 +282,10 @@ func CreateDiscountCampaign(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)
}
}()
// Insert new campaign
query := `
@@ -520,10 +520,10 @@ func UpdateDiscountCampaign(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)
}
}()
_, err = tx.Exec(r.Context(), query, args...)
if err != nil {
@@ -648,10 +648,10 @@ func DeleteDiscountCampaign(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)
}
}()
result, err := tx.Exec(r.Context(), query, campaignID)
if err != nil {
@@ -671,10 +671,12 @@ func DeleteDiscountCampaign(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": "Campaign deleted successfully",
"id": campaignID,
})
}); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
// GetCampaignStats handles GET /api/admin/discount-campaigns/{id}/stats
+18 -14
View File
@@ -75,7 +75,9 @@ func GetPatchTests(w http.ResponseWriter, r *http.Request) {
return
}
_ = json.NewEncoder(w).Encode(patchTests)
if err := json.NewEncoder(w).Encode(patchTests); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
// CreatePatchTest handles POST /api/admin/patch-tests
@@ -104,10 +106,10 @@ func CreatePatchTest(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)
}
}()
var id string
err = tx.QueryRow(r.Context(), query, req.Name, req.Description, req.NoticeDurationHours, req.ExpiryMonths, req.ServiceIDs).Scan(&id)
@@ -122,7 +124,9 @@ func CreatePatchTest(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(map[string]string{"id": id})
if err := json.NewEncoder(w).Encode(map[string]string{"id": id}); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
// UpdatePatchTest handles PUT /api/admin/patch-tests/{id}
@@ -185,10 +189,10 @@ func UpdatePatchTest(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)
}
}()
_, err = tx.Exec(r.Context(), query, args...)
if err != nil {
@@ -218,10 +222,10 @@ func DeletePatchTest(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)
}
}()
_, err = tx.Exec(r.Context(), "DELETE FROM patch_tests WHERE id = $1", id)
if err != nil {
+10 -6
View File
@@ -69,7 +69,9 @@ func GetPublicBusinessInfo(w http.ResponseWriter, r *http.Request) {
return
}
_ = json.NewEncoder(w).Encode(info)
if err := json.NewEncoder(w).Encode(info); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
func GetBusinessSettings(w http.ResponseWriter, r *http.Request) {
@@ -91,7 +93,9 @@ func GetBusinessSettings(w http.ResponseWriter, r *http.Request) {
return
}
_ = json.NewEncoder(w).Encode(s)
if err := json.NewEncoder(w).Encode(s); err != nil {
log.Printf("Failed to encode JSON response: %v", err)
}
}
type UpdateBusinessSettingsRequest struct {
@@ -246,10 +250,10 @@ func UpdateBusinessSettings(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)
}
}()
_, err = tx.Exec(r.Context(), query.String(), args...)
if err != nil {