The SuprSonic API uses conventional HTTP response codes and provides structured error messages to help you handle errors gracefully in your applications.
async function handleApiError(response) { const error = await response.json(); switch (error.error.code) { case 'AUTHENTICATION_ERROR': case 'INVALID_API_KEY': // Redirect to login or refresh API key redirectToAuth(); break; case 'AUTHORIZATION_ERROR': // Show permission denied message showError('You don\'t have permission for this action'); break; case 'VALIDATION_ERROR': // Display field-specific errors showValidationErrors(error.error.details.errors); break; case 'RATE_LIMIT_EXCEEDED': // Implement exponential backoff await retryAfterDelay(); break; default: // Generic error handling showError('Something went wrong. Please try again.'); }}
function validateArticleData(article) { const errors = []; if (!article.title || article.title.length > 500) { errors.push('Title is required and must be 500 characters or less'); } if (!article.content) { errors.push('Content is required'); } if (article.tags && article.tags.length > 20) { errors.push('Maximum 20 tags allowed'); } return errors;}// Use before API callconst validationErrors = validateArticleData(articleData);if (validationErrors.length > 0) { showErrors(validationErrors); return;}