Response Structure
Every error response contains the following fields in camelCase:Field Definitions
Error code for client-side logic (e.g.,
VALIDATION_ERROR, NOT_FOUND, UNAUTHORIZED, INTERNAL_ERROR)Human-readable message suitable for display to end users. Messages are in Spanish.
Optional field-level validation errors. Key is the field name, value is an array of error messages for that field.Only populated for
VALIDATION_ERROR responses (HTTP 400).Example:Request correlation identifier for debugging and support. Useful for tracing errors in logs.Generated from
HttpContext.TraceIdentifier.Example Error Responses
Error Response Generation
Global Exception Middleware
The API uses a global exception handling middleware (ExceptionHandlingMiddleware) that catches unhandled exceptions and converts them to standardized error responses.
Source location: HappyHabitat.API/Middleware/ExceptionHandlingMiddleware.cs
Exception mapping:
InvalidOperationException→ 400INVALID_OPERATIONArgumentException→ 400BAD_REQUESTKeyNotFoundException→ 404NOT_FOUNDUnauthorizedAccessException→ 401UNAUTHORIZEDDbUpdateException→ 500DATABASE_ERROR- All other exceptions → 500
INTERNAL_ERROR
Model Validation
Model validation errors (DataAnnotations) are automatically handled by ASP.NET Core and return aVALIDATION_ERROR response.
Configuration: Program.cs line 28-43
Controller Extension Methods
Controllers can use extension methods to return standardized error responses:HappyHabitat.API/Extensions/ControllerBaseExtensions.cs
Development vs Production
Production Message Examples
| Error Type | Production Message |
|---|---|
DATABASE_ERROR | ”Error al guardar los datos. Intente de nuevo.” |
INTERNAL_ERROR | ”Ha ocurrido un error interno. Intente más tarde.” |
Development Behavior
In development mode, the actual exception message is returned for debugging purposes.Client Implementation Guidelines
Displaying Errors
- Primary message: Display the
messagefield to users as the main error notification - Field errors: If
errorsis present, show field-specific messages next to the corresponding form inputs - Error codes: Use the
codefield for programmatic error handling (e.g., redirect to login onUNAUTHORIZED)
Using Trace IDs
- Include
traceIdin support requests for faster issue resolution - Log
traceIdin development environments for debugging - Store
traceIdtemporarily for error reporting workflows