From 6fb4823183937446c4fd5d6baacc163aad63c4a4 Mon Sep 17 00:00:00 2001 From: David Folch Agulles Date: Fri, 28 Nov 2025 18:05:25 +0100 Subject: [PATCH] Fix error response documentation for field attribute Separated ErrorResponse and ValidationErrorResponse types: - ErrorResponse: general errors (401, 403, 404, 500) - no field attribute - ValidationErrorResponse: form validation errors (400) - includes field attribute Updated respondValidationError to return appropriate type based on error format. Updated Swagger documentation to use ValidationErrorResponse only for validation endpoints. This ensures the field attribute only appears in API responses for actual form field validation errors, not in general error responses. --- internal/infrastructure/http/auth_handlers.go | 2 +- internal/infrastructure/http/dto.go | 8 ++++++-- internal/infrastructure/http/habit_handlers.go | 11 +++++++---- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/infrastructure/http/auth_handlers.go b/internal/infrastructure/http/auth_handlers.go index 8a761a4..5bda0d3 100644 --- a/internal/infrastructure/http/auth_handlers.go +++ b/internal/infrastructure/http/auth_handlers.go @@ -96,7 +96,7 @@ type LogoutRequest struct { // @Produce json // @Param request body RegisterRequest true "Registration data (password requires: min 8 chars, uppercase, lowercase, digit, special char)" // @Success 201 {object} RegisterResponse "Returns user ID and message about next steps" -// @Failure 400 {object} ErrorResponse "Invalid input: email format, password requirements, or timezone" +// @Failure 400 {object} ValidationErrorResponse "Invalid input: email format, password requirements, or timezone" // @Failure 403 {object} ErrorResponse "Registration is closed" // @Failure 409 {object} ErrorResponse "Email already registered" // @Failure 500 {object} ErrorResponse "Internal server error" diff --git a/internal/infrastructure/http/dto.go b/internal/infrastructure/http/dto.go index 2f7f06f..b207f1e 100644 --- a/internal/infrastructure/http/dto.go +++ b/internal/infrastructure/http/dto.go @@ -85,6 +85,10 @@ type HabitEntriesResponse struct { } type ErrorResponse struct { - Error string `json:"error"` - Field *string `json:"field,omitempty"` + Error string `json:"error"` +} + +type ValidationErrorResponse struct { + Error string `json:"error"` + Field string `json:"field"` } diff --git a/internal/infrastructure/http/habit_handlers.go b/internal/infrastructure/http/habit_handlers.go index 5f80fcb..385f6ed 100644 --- a/internal/infrastructure/http/habit_handlers.go +++ b/internal/infrastructure/http/habit_handlers.go @@ -594,19 +594,22 @@ func respondError(w http.ResponseWriter, status int, message string) { func respondValidationError(w http.ResponseWriter, err error) { errMsg := err.Error() - var field *string + var field string if strings.Contains(errMsg, ": ") { parts := strings.SplitN(errMsg, ": ", 3) if len(parts) >= 3 { - fieldName := parts[1] - field = &fieldName + field = parts[1] errMsg = parts[2] + respondJSON(w, http.StatusBadRequest, ValidationErrorResponse{ + Error: errMsg, + Field: field, + }) + return } } respondJSON(w, http.StatusBadRequest, ErrorResponse{ Error: errMsg, - Field: field, }) }