Error response format
The DAF Backend API returns errors in a consistent JSON format with appropriate HTTP status codes:HTTP status codes
The API uses standard HTTP status codes to indicate the type of error:400 Bad Request
400 Bad Request
The request was invalid or cannot be processed due to client error.Common causes:
- Missing required fields
- Invalid data format
- Validation errors
- Invalid query parameters
src/controllers/pos.cliente.controller.js:15-18
401 Unauthorized
401 Unauthorized
404 Not Found
404 Not Found
The requested resource does not exist.Common causes:Global 404 handler for undefined routes:
- Invalid endpoint URL
- Resource ID doesn’t exist in database
- Deleted/inactive record
src/controllers/pos.cliente.controller.js:72
src/controllers/ecom.carrito.controller.js:19-23
src/app.js:80-82
500 Internal Server Error
500 Internal Server Error
An unexpected error occurred on the server.Common causes:
- Database connection errors
- Unhandled exceptions
- Database query errors
- File system errors
src/controllers/pos.cliente.controller.js:32-34
src/controllers/ecom.carrito.controller.js:32-36
Validation errors
Validation errors occur when request data doesn’t meet the API’s requirements. The API uses two validation approaches:- Custom DTO Validation
- Zod Schema Validation
Custom validation functions
Custom DTOs return arrays of error messages:src/dtos/cliente.dto.js:3-43
Authentication errors
Authentication errors occur when JWT tokens are invalid, missing, or expired:Token no proporcionado
Token no proporcionado
Status: 401 UnauthorizedCause: The Source:Solution: Include the Authorization header with Bearer token:
Authorization header is missing from the request.Response:src/middlewares/pos.auth.middleware.js:7-11
Token inválido
Token inválido
Status: 401 UnauthorizedCause: The token format is incorrect (missing Bearer prefix or malformed).Response:Source:Solution: Ensure token is in format
src/middlewares/pos.auth.middleware.js:15-19
Bearer <token> with space after Bearer.Token inválido o expirado
Token inválido o expirado
Status: 401 UnauthorizedCause: The JWT signature is invalid, JWT_SECRET is wrong, or token has expired.Response:Source:Solutions:
src/middlewares/pos.auth.middleware.js:28-32
- Token may have expired (default 24h) - login again
- Verify
JWT_SECRETin.envhasn’t changed - Check token wasn’t corrupted during transmission
Credenciales incorrectas o error de conexión
Credenciales incorrectas o error de conexión
Status: 401 UnauthorizedCause: For POS login - database credentials are incorrect or database is unreachable.Response:Source:Solutions:
src/controllers/pos.auth.controller.js:48-51
- Verify PostgreSQL user exists and password is correct
- Check database is running and accessible
- Ensure user has CONNECT privilege on database
Database errors
Database errors typically result in 500 status codes:- Connection Errors
- Query Errors
- Connection Pool Leaks
Database connection failures
Common errors:ECONNREFUSED- PostgreSQL not running or wrong portConnection terminated unexpectedly- Database crashedpassword authentication failed- Wrong credentialsdatabase does not exist- Database name incorrect
src/controllers/pos.cliente.controller.js:75-80
- Check server logs for detailed error messages
- Verify database connection settings in
.env - Test database connectivity:
psql -h localhost -p 5432 -U user -d database
File upload errors
File upload errors occur when handling product images:Solo se permiten imágenes
Solo se permiten imágenes
Status: 500 Internal Server ErrorCause: Uploaded file is not an image type.Response:Source:Solution: Only upload image files (JPEG, PNG, GIF, WebP, etc.)
src/middlewares/upload.js:22-27
File size too large
File size too large
Status: 500 Internal Server ErrorCause: Uploaded file exceeds 5MB limit.Configuration:Solution: Compress or resize images before uploading.
src/middlewares/upload.js:29-33
Directory not writable
Directory not writable
Status: 500 Internal Server ErrorCause: The Solution: Ensure the Node.js process has write permissions:
src/images directory doesn’t exist or lacks write permissions.Prevention: Upload middleware creates directory if missing:src/middlewares/upload.js:5-9
Common client-side errors
Missing required parameters
Example:Always check API documentation for required parameters.
src/controllers/pos.producto.controller.js:111-113
Invalid ID format
Example:Ensure IDs match expected format (usually VARCHAR(10)).
src/controllers/pos.producto.controller.js:143-145
CORS errors
If frontend origin is not in allowed list:Add your frontend URL to
src/app.js:29-35
FRONTEND_IP in .env.Pagination errors
Invalid page numbers default to 1:Use positive integers for page numbers.
src/controllers/pos.cliente.controller.js:47
Error handling best practices
Return appropriate status codes
Use the correct HTTP status code:
- 400 for validation errors
- 401 for authentication errors
- 404 for not found
- 500 for server errors
Debugging tips
Check server logs
Check server logs
The API logs errors to console. Run the server and watch for error output:Look for:
console.error()messages from controllers- Database connection errors
- Middleware errors
Test database connectivity
Test database connectivity
Verify database connections separately:
Validate JWT tokens
Validate JWT tokens
Debug JWT tokens at jwt.io:
- Copy your token
- Paste into jwt.io debugger
- Verify payload and expiration
- Check signature with your
JWT_SECRET
Test with cURL
Test with cURL
Isolate issues by testing with cURL:The
-v flag shows full request/response details.Check environment variables
Check environment variables
Verify Remove after debugging.
.env is loaded correctly:Add temporary debug output:Next steps
POS API Reference
See all endpoints with request/response examples
Authentication
Understand authentication error scenarios
Database
Learn about database connection errors
Architecture
Understand the error handling flow