Error Handling
The Sales Management System API uses consistent error response patterns to help you handle failures gracefully.Error Response Format
All error responses follow this structure:HTTP Status Codes
The API uses standard HTTP status codes:| Status Code | Meaning | When Used |
|---|---|---|
200 | Success | Operation completed successfully |
400 | Bad Request | Invalid input, validation failed |
404 | Not Found | Resource doesn’t exist |
409 | Conflict | Duplicate entry, foreign key constraint |
500 | Server Error | Database query failed, internal error |
Common Errors
Validation Errors
Occur when input doesn’t meet validation requirements.Empty Request Body
Request:400 Bad Request
Solution: Provide required fields in the request body.
Invalid Field Values
Example: Category name too short Request:400 Bad Request
Validation Rules:
- Category name: minimum 3 characters
- Product name: minimum 3 characters
- Product description: minimum 10 characters
- Client name: minimum 3 characters
- Email: valid email format
- Stock: non-negative integer
- Price: positive decimal
- Quantity: positive integer
Validation is performed using Zod schemas in
src/controller/validation/.Duplicate Entry Errors
Occur when trying to create a record with a value that must be unique.Duplicate Category Name
Request:409 Conflict
Solution: Use a different category name.
Duplicate Client Email
Request:409 Conflict
Unique Constraints:
categories.nameclients.emailusers.emailroles.name
Foreign Key Constraint Violations
Occur when trying to delete a record that other records depend on.Cannot Delete Category with Products
From the README.md example: Request:409 Conflict
Solution:
- Update products to a different category
- Delete the products first
- Or keep the category
src/model/categorie.model.js:30-48):
Cannot Delete Product with Sales
From the README.md example: Request:409 Conflict
Solution:
- Products with sales cannot be deleted (data integrity)
- Use the
is_deletedflag for soft deletion instead
Cannot Delete Client with Sales
From the README.md example: Request:409 Conflict
Solution:
- Clients with sales history cannot be deleted
- This preserves historical sales data
Stock Validation Errors
Occur when trying to sell more items than available in stock. Request:400 Bad Request
Solution: Reduce quantity to available stock or restock the product.
Resource Not Found Errors
Occur when requesting a non-existent resource. Request:404 Not Found (or 400 depending on implementation)
Common scenarios:
- Invalid category ID
- Invalid product UUID
- Invalid client UUID
- Invalid sale UUID
Database Query Errors
Occur when database operations fail. Response:500 Internal Server Error
Common causes:
- Database connection lost
- MySQL server down
- Table doesn’t exist
- Insufficient permissions
- Check database connection in
src/config.js - Verify MySQL is running:
sudo systemctl status mysql - Check database exists:
SHOW DATABASES; - Verify user permissions:
SHOW GRANTS FOR 'user'@'localhost';
Error Handling in Code
Controller Layer
Controllers validate input and handle model errors:Model Layer
Models catch database errors and return standardized responses:Validation Layer
Zod schemas validate data types and constraints:safeParseAsync doesn’t throw exceptions, making error handling cleaner.Client-Side Error Handling
JavaScript/Node.js Example
Python Example
cURL with Error Checking
Error Recovery Strategies
Validation Errors (400)
Validation Errors (400)
Strategy: Fix the input and retry
- Check validation rules in the documentation
- Ensure all required fields are present
- Verify data types and formats
- Retry with corrected data
Duplicate Entries (409)
Duplicate Entries (409)
Strategy: Use a different value or update existing record
- Check if the record already exists (GET request)
- If it exists and is correct, use it
- If it exists but needs changes, use UPDATE (PATCH)
- If it must be unique, choose a different value
Foreign Key Violations (409)
Foreign Key Violations (409)
Strategy: Handle dependencies before deletion
- Check for dependent records (the error includes IDs)
- Update or delete dependent records first
- Or keep the parent record if dependencies exist
- Consider soft deletion instead of hard deletion
Resource Not Found (404)
Resource Not Found (404)
Strategy: Verify the ID and handle gracefully
- Check if the ID/UUID is correct
- Verify the resource exists (GET request)
- Handle missing resources in your UI/logic
- Don’t assume resources exist
Database Errors (500)
Database Errors (500)
Strategy: Check connection and retry
- Verify database is running
- Check network connectivity
- Implement exponential backoff retry logic
- Log errors for investigation
- Consider circuit breaker pattern for production
Best Practices
Always Check the error Field
Every response includes
"error": true/false. Always check this before processing data:Testing Error Scenarios
Test your error handling with these scenarios:Next Steps
API Reference
See all endpoints and their error responses
Database Schema
Understand foreign key relationships
Architecture
Learn how errors flow through the system
Configuration
Fix database connection errors