Error Handling
Error Handling
Learn how to handle errors in Mavibase API calls.
HTTP Status Codes
2xx Success
- 200 OK: Request successful
- 201 Created: Resource created
- 204 No Content: Success, no content to return
4xx Client Errors
- 400 Bad Request: Invalid request format
- 401 Unauthorized: Missing or invalid authentication
- 403 Forbidden: Insufficient permissions
- 404 Not Found: Resource not found
- 409 Conflict: Request conflicts with current state
- 422 Unprocessable Entity: Validation failed
- 429 Too Many Requests: Rate limited
5xx Server Errors
- 500 Internal Server Error: Server error
- 502 Bad Gateway: Temporary service issue
- 503 Service Unavailable: Service temporarily unavailable
Error Response Format
javascript
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Email must be a valid email address",
"details": [
{
"field": "email",
"message": "Invalid email format"
}
]
}
}Common Error Codes
Authentication
AUTHENTICATION_FAILED: Invalid credentialsTOKEN_EXPIRED: Session token expiredINVALID_API_KEY: API key is invalidAPI_KEY_REVOKED: API key has been revoked
Authorization
INSUFFICIENT_PERMISSIONS: User lacks required permissionsRESOURCE_FORBIDDEN: User cannot access resource
Validation
VALIDATION_ERROR: Request validation failedINVALID_FIELD_TYPE: Field has wrong typeREQUIRED_FIELD_MISSING: Required field missingUNIQUE_CONSTRAINT_VIOLATION: Value must be unique
Database
COLLECTION_NOT_FOUND: Collection doesn't existDOCUMENT_NOT_FOUND: Document doesn't existDUPLICATE_KEY: Unique constraint violated
Transaction
TRANSACTION_NOT_FOUND: Transaction doesn't existTRANSACTION_TIMEOUT: Transaction exceeded timeoutCONFLICT: Document modified by another transactionDEADLOCK: Circular transaction dependency
Rate Limiting
RATE_LIMITED: Request rate limit exceededQUOTA_EXCEEDED: Usage quota exceeded
Handling Errors
Try-Catch Pattern
javascript
try {
const user = await client.documents.get({
collection: 'users',
id: 'user_123'
});
} catch (error) {
if (error.code === 'DOCUMENT_NOT_FOUND') {
console.log('User not found');
} else if (error.code === 'INSUFFICIENT_PERMISSIONS') {
console.log('Access denied');
} else {
console.error('Unexpected error:', error);
}
}Retry with Exponential Backoff
javascript
async function withRetry(fn, maxRetries = 3) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return await fn();
} catch (error) {
// Retry on server errors and rate limiting
const retryable = error.status >= 500 || error.code === 'RATE_LIMITED';
if (!retryable || attempt === maxRetries - 1) {
throw error;
}
const delay = Math.pow(2, attempt) * 1000; // 1s, 2s, 4s
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
// Usage
const user = await withRetry(() =>
client.documents.get({
collection: 'users',
id: 'user_123'
})
);Best Practices
- Handle Specific Errors: Check error codes, not just messages
- Implement Retry Logic: For transient failures
- Log Errors: Include request IDs for debugging
- User-Friendly Messages: Don't expose internal details
- Rate Limit Handling: Respect
Retry-Afterheaders - Timeout Handling: Set appropriate timeouts
- Validate Input: Prevent validation errors
- Test Error Paths: Verify error handling works