fix: return field-level validation errors on POST /habits

Previously all validation failures in CreateHabitHandler returned a
generic {"error":"invalid input"}, making it impossible to tell
whether type, frequency, specific_days or specific_dates was the
problem. Errors are now wrapped with field + i18n key and the HTTP
layer replies via respondValidationErrorI18n, matching the pattern
already used by auth endpoints.
This commit is contained in:
2026-04-17 19:04:23 +02:00
parent c20720f120
commit ed50d2427e
4 changed files with 91 additions and 14 deletions
@@ -2,6 +2,7 @@ package http
import (
"encoding/json"
stderrors "errors"
"net/http"
"strconv"
"strings"
@@ -97,8 +98,8 @@ func (h *HabitHandlers) CreateHabit(w http.ResponseWriter, r *http.Request) {
habitID, err := h.createHandler.Handle(r.Context(), cmd)
if err != nil {
if err == errors.ErrInvalidInput {
respondError(w, http.StatusBadRequest, err.Error())
if stderrors.Is(err, errors.ErrInvalidInput) {
respondValidationErrorI18n(w, r, h.translator, err)
return
}
respondErrorI18n(w, r, h.translator, http.StatusInternalServerError, "failed_create_habit")