mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-06-06 17:12:42 -07:00
fix(cli): implement robust error extraction logic and tests for useBtw
This commit is contained in:
committed by
Mahima Shanware
parent
909c35c2d5
commit
12c4d27506
@@ -91,6 +91,66 @@ describe('useBtw', () => {
|
||||
expect(result.current.isStreaming).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle string errors', async () => {
|
||||
const mockStream = (async function* () {
|
||||
yield {
|
||||
type: GeminiEventType.Error,
|
||||
value: { error: 'Direct string error' },
|
||||
};
|
||||
})();
|
||||
mockGeminiClient.sendBtwStream.mockReturnValue(mockStream);
|
||||
|
||||
const { result } = await renderHook(() =>
|
||||
useBtw(mockGeminiClient as unknown as GeminiClient),
|
||||
);
|
||||
|
||||
await act(async () => {
|
||||
await result.current.submitBtw('test query');
|
||||
});
|
||||
|
||||
expect(result.current.error).toBe('Direct string error');
|
||||
});
|
||||
|
||||
it('should handle whitespace errors by falling back to Unknown error', async () => {
|
||||
const mockStream = (async function* () {
|
||||
yield {
|
||||
type: GeminiEventType.Error,
|
||||
value: { error: { message: ' ' } },
|
||||
};
|
||||
})();
|
||||
mockGeminiClient.sendBtwStream.mockReturnValue(mockStream);
|
||||
|
||||
const { result } = await renderHook(() =>
|
||||
useBtw(mockGeminiClient as unknown as GeminiClient),
|
||||
);
|
||||
|
||||
await act(async () => {
|
||||
await result.current.submitBtw('test query');
|
||||
});
|
||||
|
||||
expect(result.current.error).toBe('Unknown error');
|
||||
});
|
||||
|
||||
it('should handle unknown error shapes', async () => {
|
||||
const mockStream = (async function* () {
|
||||
yield {
|
||||
type: GeminiEventType.Error,
|
||||
value: 'Just some raw string value',
|
||||
};
|
||||
})();
|
||||
mockGeminiClient.sendBtwStream.mockReturnValue(mockStream);
|
||||
|
||||
const { result } = await renderHook(() =>
|
||||
useBtw(mockGeminiClient as unknown as GeminiClient),
|
||||
);
|
||||
|
||||
await act(async () => {
|
||||
await result.current.submitBtw('test query');
|
||||
});
|
||||
|
||||
expect(result.current.error).toBe('Just some raw string value');
|
||||
});
|
||||
|
||||
it('should reset state on dismiss', async () => {
|
||||
const mockStream = (async function* () {
|
||||
yield { type: GeminiEventType.Content, value: 'partial' };
|
||||
|
||||
@@ -150,17 +150,18 @@ export const useBtw = (
|
||||
if (
|
||||
typeof value === 'object' &&
|
||||
value !== null &&
|
||||
'error' in value &&
|
||||
typeof value.error === 'object' &&
|
||||
value.error !== null
|
||||
'error' in value
|
||||
) {
|
||||
const errorObj = value.error;
|
||||
errorMessage =
|
||||
const extracted =
|
||||
typeof errorObj === 'object' &&
|
||||
errorObj !== null &&
|
||||
'message' in errorObj
|
||||
? String(errorObj.message)
|
||||
: String(errorObj);
|
||||
errorMessage = extracted.trim() || 'Unknown error';
|
||||
} else {
|
||||
errorMessage = String(value);
|
||||
errorMessage = String(value).trim() || 'Unknown error';
|
||||
}
|
||||
dispatch({
|
||||
type: 'ERROR',
|
||||
|
||||
Reference in New Issue
Block a user