diff options
Diffstat (limited to 'packages/cli/src/ui/hooks')
| -rw-r--r-- | packages/cli/src/ui/hooks/useGeminiStream.test.tsx | 58 | ||||
| -rw-r--r-- | packages/cli/src/ui/hooks/useGeminiStream.ts | 13 |
2 files changed, 64 insertions, 7 deletions
diff --git a/packages/cli/src/ui/hooks/useGeminiStream.test.tsx b/packages/cli/src/ui/hooks/useGeminiStream.test.tsx index 29871e8a..ed1eea4d 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.test.tsx +++ b/packages/cli/src/ui/hooks/useGeminiStream.test.tsx @@ -16,7 +16,7 @@ import { TrackedExecutingToolCall, TrackedCancelledToolCall, } from './useReactToolScheduler.js'; -import { Config, EditorType } from '@gemini-cli/core'; +import { Config, EditorType, AuthType } from '@gemini-cli/core'; import { Part, PartListUnion } from '@google/genai'; import { UseHistoryManagerReturn } from './useHistoryManager.js'; import { HistoryItem, MessageType, StreamingState } from '../types.js'; @@ -117,6 +117,11 @@ vi.mock('./slashCommandProcessor.js', () => ({ handleSlashCommand: vi.fn().mockReturnValue(false), })); +const mockParseAndFormatApiError = vi.hoisted(() => vi.fn()); +vi.mock('../utils/errorParsing.js', () => ({ + parseAndFormatApiError: mockParseAndFormatApiError, +})); + // --- END MOCKS --- describe('mergePartListUnions', () => { @@ -1033,4 +1038,55 @@ describe('useGeminiStream', () => { }); }); }); + + describe('Error Handling', () => { + it('should call parseAndFormatApiError with the correct authType on stream initialization failure', async () => { + // 1. Setup + const mockError = new Error('Rate limit exceeded'); + const mockAuthType = AuthType.LOGIN_WITH_GOOGLE_ENTERPRISE; + mockParseAndFormatApiError.mockClear(); + mockSendMessageStream.mockReturnValue( + (async function* () { + yield { type: 'content', value: '' }; + throw mockError; + })(), + ); + + const testConfig = { + ...mockConfig, + getContentGeneratorConfig: vi.fn(() => ({ + authType: mockAuthType, + })), + } as unknown as Config; + + const { result } = renderHook(() => + useGeminiStream( + new MockedGeminiClientClass(testConfig), + [], + mockAddItem, + mockSetShowHelp, + testConfig, + mockOnDebugMessage, + mockHandleSlashCommand, + false, + () => 'vscode' as EditorType, + () => {}, + () => Promise.resolve(), + ), + ); + + // 2. Action + await act(async () => { + await result.current.submitQuery('test query'); + }); + + // 3. Assertion + await waitFor(() => { + expect(mockParseAndFormatApiError).toHaveBeenCalledWith( + 'Rate limit exceeded', + mockAuthType, + ); + }); + }); + }); }); diff --git a/packages/cli/src/ui/hooks/useGeminiStream.ts b/packages/cli/src/ui/hooks/useGeminiStream.ts index 86540b68..a8816f98 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.ts +++ b/packages/cli/src/ui/hooks/useGeminiStream.ts @@ -398,12 +398,15 @@ export const useGeminiStream = ( addItem( { type: MessageType.ERROR, - text: parseAndFormatApiError(eventValue.error), + text: parseAndFormatApiError( + eventValue.error, + config.getContentGeneratorConfig().authType, + ), }, userMessageTimestamp, ); }, - [addItem, pendingHistoryItemRef, setPendingHistoryItem], + [addItem, pendingHistoryItemRef, setPendingHistoryItem, config], ); const handleChatCompressionEvent = useCallback( @@ -533,10 +536,6 @@ export const useGeminiStream = ( setPendingHistoryItem(null); } } catch (error: unknown) { - console.log( - 'GEMINI_DEBUG: Caught error in useGeminiStream.ts:', - JSON.stringify(error), - ); if (error instanceof UnauthorizedError) { onAuthError(); } else if (!isNodeError(error) || error.name !== 'AbortError') { @@ -545,6 +544,7 @@ export const useGeminiStream = ( type: MessageType.ERROR, text: parseAndFormatApiError( getErrorMessage(error) || 'Unknown error', + config.getContentGeneratorConfig().authType, ), }, userMessageTimestamp, @@ -566,6 +566,7 @@ export const useGeminiStream = ( geminiClient, startNewTurn, onAuthError, + config, ], ); |
