元渊 API元渊 API
Error Handling Guide

FAQ

Frequently asked questions about error handling

Detection Questions

Q: Why check candidatesTokenCount first?

A: It's the earliest rejection point. When content is rejected at review stage:

  • candidatesTokenCount is directly 0
  • Other fields may look "normal" (e.g., finishReason: "STOP")
  • Most reliable indicator

Q: Does finishReason = STOP mean success?

A: Not always. Must check other fields:

// ❌ Wrong
if (finishReason === 'STOP') return 'success';

// ✅ Correct
if (finishReason === 'STOP' && 
    candidatesTokenCount > 0 && 
    hasImageData) {
    return 'success';
}

Q: Are text responses always errors?

A: Not always. Need to detect rejection patterns:

if (text.includes("I can't generate")) {
    return { error: true }; // Rejection
}

Handling Questions

Q: Should all errors show modals?

A: No. Choose display method by type:

Error TypeDisplayBlocking
Content ReviewModalYes
Generation RejectionModalYes
Text ResponseInlineYes
Network ErrorToastNo

Q: How to handle repeated failures?

A: Implement fallback strategy:

if (retryCount >= 3) {
    showAlternatives({
        message: 'Multiple failures detected',
        options: [
            'View Content Guidelines',
            'Contact Support',
            'Try Different Model'
        ]
    });
}

Implementation Questions

Q: How to test error logic?

A: Use mock data:

const mockResponses = {
    zeroCandidatesToken: {
        candidates: [{ content: { parts: [] } }],
        usageMetadata: { candidatesTokenCount: 0 }
    },
    // ... more cases
};

test('detects zero candidates token', () => {
    const result = processResponse(mockResponses.zeroCandidatesToken);
    expect(result.errorType).toBe('ZERO_CANDIDATES_TOKEN');
});

How is this guide?