Overview
Kontrak Backend uses structured error responses to help you identify and resolve issues quickly. All errors follow a consistent format and include detailed information for debugging.Error Response Structure
All API errors return a JSON response with the following structure:HTTP Status Codes
The API uses standard HTTP status codes defined insrc/constants/http.ts:
| Status Code | Constant | Meaning | When Used |
|---|---|---|---|
| 200 | OK | Success | Successful requests |
| 201 | CREATED | Created | Resource created successfully |
| 400 | BAD_REQUEST | Bad Request | Invalid input or validation errors |
| 401 | UNAUTHORIZED | Unauthorized | Authentication required |
| 403 | FORBIDDEN | Forbidden | Insufficient permissions |
| 404 | NOT_FOUND | Not Found | Resource not found |
| 409 | CONFLICT | Conflict | Resource conflict |
| 422 | UNPROCESSABLE_CONTENT | Unprocessable | Semantic errors |
| 429 | TOO_MANY_REQUESTS | Rate Limited | Too many requests |
| 500 | INTERNAL_SERVER_ERROR | Server Error | Unexpected server error |
Error Types
Application Errors (AppError)
Structured errors with specific status codes and context:Validation Errors
Validation errors include detailed information about which fields failed validation:src/services/excel-parser.service.ts:58
File Upload Errors (Multer)
File upload errors are handled by the Multer middleware (src/middlewares/error-handler-multer.middleware.ts):
LIMIT_FILE_SIZE
LIMIT_FILE_SIZE
Status Code: 400Response:Solution:
- Reduce file size
- Split data into multiple files
- Increase
MAX_FILE_SIZEin configuration
LIMIT_FILE_COUNT
LIMIT_FILE_COUNT
Status Code: 400Response:Solution:
- Upload one file at a time
- Combine data into single Excel file
LIMIT_UNEXPECTED_FILE
LIMIT_UNEXPECTED_FILE
Status Code: 400Response:Solution:
- Use field name
excelfor file upload - Check multipart form field name
Invalid File Type
Invalid File Type
Status Code: 400Response:Solution:
- Use .xlsx file format
- Convert .xls files to .xlsx
- Ensure proper MIME type
Common Error Scenarios
Excel File Errors
- Missing Headers
- Corrupt File
- Empty File
Error:Debugging Steps:
- Check first row contains headers
- Verify header spelling matches accepted aliases
- Remove extra spaces in header names
- Check for special characters in headers
- Add missing column headers
- Use one of the accepted aliases from field configuration
- See Excel Upload Guide for valid headers
Validation Errors
- DNI Validation
- Email Validation
- Salary Validation
- Date Validation
Common Errors:Validation Rule:Valid Examples:
- ✅
12345678 - ✅
87654321
- ❌
1234567(too short) - ❌
123456789(too long) - ❌
12-345-678(contains dashes) - ❌
12.345.678(contains dots)
Contract Type Errors
- Provide all required SUBSIDIO fields
- See Generating Contracts Guide
Session and Resource Errors
- Session Not Found
- PDF Not Found
Error:Status Code: 404Causes:
- Session expired or cleaned up
- Invalid session ID
- Session directory deleted
- Re-upload Excel file to create new session
- Check
FILE_CLEANUP_TIMEOUTconfiguration - Verify session ID is correct
Error Handler Middleware
The global error handler (src/middlewares/error-handle.middleware.ts) catches all errors:
AppError Handling
Debugging Tips
Check Error Response
Read the error message and status code:Look for:
- HTTP status code in response headers
success: falsein JSON bodymessagefield with error descriptiondatafield with additional context
Review Server Logs
Check server console for detailed error information:The logger outputs:
- Error stack traces
- Validation details
- File processing steps
- CORS blocking messages
Validate Input Data
Verify your input matches expected format:
- Excel files: Check headers and data types
- JSON requests: Validate against schema
- File uploads: Verify size and format
- Dates: Use DD/MM/YYYY format
Error Prevention
Input Validation
- Validate data before upload
- Use provided Excel templates
- Check required fields are present
- Test with small batches first
File Preparation
- Use .xlsx format
- Keep files under size limit
- Remove unnecessary formatting
- Verify headers match aliases
API Usage
- Check response status codes
- Handle errors gracefully
- Implement retry logic for transient errors
- Log errors for debugging
Configuration
- Set appropriate file size limits
- Configure CORS correctly
- Monitor disk space for temp files
- Set reasonable timeouts
HTTP Client Error Handling Examples
Next Steps
Configuration
Configure error handling and logging
Excel Upload
Learn proper Excel file format