元渊 API元渊 API
Error Handling Guide

Best Practices

Recommended patterns for error handling

Core Principles

1. User Experience First

  • Never expose technical errors directly
  • Use positive, constructive language
  • Provide actionable suggestions

2. Layered Error Handling

  1. Frontend validation - Check input before API call
  2. API response parsing - Detect error types
  3. User presentation - Display friendly messages

3. Comprehensive Logging

function logError(error, context) {
    console.log({
        timestamp: new Date().toISOString(),
        errorType: error.errorType,
        userMessage: error.userMessage,
        userId: context.userId,
        prompt: context.prompt,
        rawResponse: error.rawResponse
    });
}

4. Retry Strategy

async function retryWithBackoff(fn, maxRetries = 3) {
    for (let attempt = 0; attempt < maxRetries; attempt++) {
        try {
            return await fn();
        } catch (error) {
            if (attempt === maxRetries - 1) throw error;
            await sleep(Math.pow(2, attempt) * 1000);
        }
    }
}

Key Recommendations

  1. Always check candidatesTokenCount first
  2. Log all errors for monitoring
  3. Provide clear user guidance
  4. Implement automatic retry for transient errors
  5. Use A/B testing for error messages

How is this guide?