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.
This commit is contained in:
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user