summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/utils/errorParsing.test.ts
diff options
context:
space:
mode:
authorAbhi <[email protected]>2025-06-23 17:30:13 -0400
committerGitHub <[email protected]>2025-06-23 21:30:13 +0000
commitdc76bcc433d58d879f8850ac777d2cd239dad611 (patch)
tree89458926149f83721f00f09a5b5b3130d571bda5 /packages/cli/src/ui/utils/errorParsing.test.ts
parent21e6a36cf1b17ff126b3d0253e68a6f3ebfc7c36 (diff)
Add error messaging for 429 errors (#1316)
Diffstat (limited to 'packages/cli/src/ui/utils/errorParsing.test.ts')
-rw-r--r--packages/cli/src/ui/utils/errorParsing.test.ts55
1 files changed, 48 insertions, 7 deletions
diff --git a/packages/cli/src/ui/utils/errorParsing.test.ts b/packages/cli/src/ui/utils/errorParsing.test.ts
index afee5793..0dbd75c8 100644
--- a/packages/cli/src/ui/utils/errorParsing.test.ts
+++ b/packages/cli/src/ui/utils/errorParsing.test.ts
@@ -6,29 +6,46 @@
import { describe, it, expect } from 'vitest';
import { parseAndFormatApiError } from './errorParsing.js';
+import { StructuredError } from '@gemini-cli/core';
describe('parseAndFormatApiError', () => {
+ const rateLimitMessage =
+ 'Please wait and try again later. To increase your limits, upgrade to a plan with higher limits, or use /auth to switch to using a paid API key from AI Studio at https://aistudio.google.com/apikey';
+
it('should format a valid API error JSON', () => {
const errorMessage =
'got status: 400 Bad Request. {"error":{"code":400,"message":"API key not valid. Please pass a valid API key.","status":"INVALID_ARGUMENT"}}';
const expected =
- 'API Error: API key not valid. Please pass a valid API key. (Status: INVALID_ARGUMENT)';
+ '[API Error: API key not valid. Please pass a valid API key. (Status: INVALID_ARGUMENT)]';
+ expect(parseAndFormatApiError(errorMessage)).toBe(expected);
+ });
+
+ it('should format a 429 API error JSON with the custom message', () => {
+ const errorMessage =
+ 'got status: 429 Too Many Requests. {"error":{"code":429,"message":"Rate limit exceeded","status":"RESOURCE_EXHAUSTED"}}';
+ const expected = `[API Error: Rate limit exceeded (Status: RESOURCE_EXHAUSTED)]\n${rateLimitMessage}`;
expect(parseAndFormatApiError(errorMessage)).toBe(expected);
});
it('should return the original message if it is not a JSON error', () => {
const errorMessage = 'This is a plain old error message';
- expect(parseAndFormatApiError(errorMessage)).toBe(errorMessage);
+ expect(parseAndFormatApiError(errorMessage)).toBe(
+ `[API Error: ${errorMessage}]`,
+ );
});
it('should return the original message for malformed JSON', () => {
const errorMessage = '[Stream Error: {"error": "malformed}';
- expect(parseAndFormatApiError(errorMessage)).toBe(errorMessage);
+ expect(parseAndFormatApiError(errorMessage)).toBe(
+ `[API Error: ${errorMessage}]`,
+ );
});
it('should handle JSON that does not match the ApiError structure', () => {
const errorMessage = '[Stream Error: {"not_an_error": "some other json"}]';
- expect(parseAndFormatApiError(errorMessage)).toBe(errorMessage);
+ expect(parseAndFormatApiError(errorMessage)).toBe(
+ `[API Error: ${errorMessage}]`,
+ );
});
it('should format a nested API error', () => {
@@ -49,8 +66,32 @@ describe('parseAndFormatApiError', () => {
},
});
- expect(parseAndFormatApiError(errorMessage)).toBe(
- "API Error: Gemini 2.5 Pro Preview doesn't have a free quota tier. For more information on this error, head to: https://ai.google.dev/gemini-api/docs/rate-limits. (Status: Too Many Requests)",
- );
+ const expected = `[API Error: Gemini 2.5 Pro Preview doesn't have a free quota tier. For more information on this error, head to: https://ai.google.dev/gemini-api/docs/rate-limits. (Status: Too Many Requests)]\n${rateLimitMessage}`;
+
+ expect(parseAndFormatApiError(errorMessage)).toBe(expected);
+ });
+
+ it('should format a StructuredError', () => {
+ const error: StructuredError = {
+ message: 'A structured error occurred',
+ status: 500,
+ };
+ const expected = '[API Error: A structured error occurred]';
+ expect(parseAndFormatApiError(error)).toBe(expected);
+ });
+
+ it('should format a 429 StructuredError with the custom message', () => {
+ const error: StructuredError = {
+ message: 'Rate limit exceeded',
+ status: 429,
+ };
+ const expected = `[API Error: Rate limit exceeded]\n${rateLimitMessage}`;
+ expect(parseAndFormatApiError(error)).toBe(expected);
+ });
+
+ it('should handle an unknown error type', () => {
+ const error = 12345;
+ const expected = '[API Error: An unknown error occurred.]';
+ expect(parseAndFormatApiError(error)).toBe(expected);
});
});