API Error Handling
BuildAppsWith implements comprehensive error handling across all API endpoints using standardized error codes, consistent response formats, and type-safe error interfaces.Error Response Structure
All API errors follow theStandardApiResponse pattern with a structured error object:
Standard Error Codes
Client Errors (4xx)
Request is malformed, missing required parameters, or contains invalid data
Authentication is required but not provided or invalid
User is authenticated but lacks permission for the requested resource
The requested resource does not exist
Request data fails validation rules
Request conflicts with current resource state
Rate limit exceeded for the requesting client
Server Errors (5xx)
Unexpected server error occurred
Service temporarily unavailable
Error Handling in API Routes
Basic Error Response
Validation Error Response
Not Found Error Response
Client-Side Error Handling
API Client Functions
React Component Error Handling
Global Error Handling Hook
Error Monitoring and Logging
Server-Side Error Logging
Client-Side Error Reporting
Testing Error Responses
Unit Testing API Routes
Integration Testing
Best Practices
1. Consistent Error Responses
- Always use
toStandardResponse()for error responses - Include appropriate HTTP status codes
- Provide meaningful error messages for users
2. Security Considerations
- Don’t expose sensitive information in error messages
- Sanitize error details in production environments
- Use generic messages for internal server errors
3. Error Recovery
- Implement retry logic for transient failures
- Provide clear actions users can take to resolve errors
- Use fallback data when appropriate
4. Monitoring and Alerting
- Log all errors for debugging and monitoring
- Set up alerts for high error rates
- Track error trends and patterns