Error Handling
This guide covers all possible errors you might encounter when using the Nexus API and provides best practices for handling them gracefully in your applications.Error Response Format
All API errors follow a consistent format to make error handling predictable:HTTP Status Codes
The Nexus API uses standard HTTP status codes to indicate the success or failure of requests:| Status Code | Meaning | Description |
|---|---|---|
| 200 | Success | Request completed successfully |
| 400 | Bad Request | Invalid request parameters or malformed request body |
| 401 | Unauthorized | Missing or invalid API key |
| 403 | Forbidden | Valid API key but insufficient permissions |
| 404 | Not Found | Resource not found (e.g., session doesn’t exist) |
| 413 | Payload Too Large | Request body exceeds size limits |
| 429 | Too Many Requests | Rate limit exceeded |
| 500 | Internal Server Error | Unexpected server error |
| 503 | Service Unavailable | Temporary service outage |
Common Error Scenarios
Authentication Errors (401)
Session Not Found (404)
Rate Limiting (429)
Validation Errors (400)
Error Recovery Strategies
1. Circuit Breaker Pattern
2. Fallback Mechanisms
3. Graceful Degradation
Best Practices
1. Always Handle Errors Explicitly
1. Always Handle Errors Explicitly
- Never ignore errors or use empty catch blocks
- Log errors with context for debugging
- Provide meaningful error messages to users
- Distinguish between recoverable and non-recoverable errors
2. Implement Retry Logic
2. Implement Retry Logic
- Use exponential backoff for transient failures
- Set reasonable retry limits
- Add jitter to prevent thundering herd
- Respect Retry-After headers
3. Client-Side Validation
3. Client-Side Validation
- Validate inputs before making API calls
- Check message length limits
- Verify session ID format
- Sanitize user inputs
4. Monitor and Alert
4. Monitor and Alert
- Track error rates and patterns
- Set up alerts for error spikes
- Monitor specific error codes
- Use error tracking services
5. User Experience
5. User Experience
- Show user-friendly error messages
- Provide actionable next steps
- Offer retry options when appropriate
- Maintain application state during errors