feat: add custom message for 429 errors (#1366)

This commit is contained in:
Abhi
2025-06-23 23:43:00 -04:00
committed by GitHub
parent f7caca5f94
commit d3f13c71ae
6 changed files with 133 additions and 25 deletions

View File

@@ -6,11 +6,12 @@
import { describe, it, expect } from 'vitest';
import { parseAndFormatApiError } from './errorParsing.js';
import { StructuredError } from '@gemini-cli/core';
import { AuthType, 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';
const enterpriseMessage = 'upgrade to a plan with higher limits';
const vertexMessage = 'request a quota increase through Vertex';
const geminiMessage = 'request a quota increase through AI Studio';
it('should format a valid API error JSON', () => {
const errorMessage =
@@ -20,11 +21,31 @@ describe('parseAndFormatApiError', () => {
expect(parseAndFormatApiError(errorMessage)).toBe(expected);
});
it('should format a 429 API error JSON with the custom message', () => {
it('should format a 429 API error with the default 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);
const result = parseAndFormatApiError(errorMessage);
expect(result).toContain('[API Error: Rate limit exceeded');
expect(result).toContain('Your request has been rate limited');
});
it('should format a 429 API error with the enterprise message', () => {
const errorMessage =
'got status: 429 Too Many Requests. {"error":{"code":429,"message":"Rate limit exceeded","status":"RESOURCE_EXHAUSTED"}}';
const result = parseAndFormatApiError(
errorMessage,
AuthType.LOGIN_WITH_GOOGLE_ENTERPRISE,
);
expect(result).toContain('[API Error: Rate limit exceeded');
expect(result).toContain(enterpriseMessage);
});
it('should format a 429 API error with the vertex message', () => {
const errorMessage =
'got status: 429 Too Many Requests. {"error":{"code":429,"message":"Rate limit exceeded","status":"RESOURCE_EXHAUSTED"}}';
const result = parseAndFormatApiError(errorMessage, AuthType.USE_VERTEX_AI);
expect(result).toContain('[API Error: Rate limit exceeded');
expect(result).toContain(vertexMessage);
});
it('should return the original message if it is not a JSON error', () => {
@@ -66,9 +87,9 @@ describe('parseAndFormatApiError', () => {
},
});
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);
const result = parseAndFormatApiError(errorMessage, AuthType.USE_GEMINI);
expect(result).toContain('Gemini 2.5 Pro Preview');
expect(result).toContain(geminiMessage);
});
it('should format a StructuredError', () => {
@@ -80,13 +101,14 @@ describe('parseAndFormatApiError', () => {
expect(parseAndFormatApiError(error)).toBe(expected);
});
it('should format a 429 StructuredError with the custom message', () => {
it('should format a 429 StructuredError with the vertex message', () => {
const error: StructuredError = {
message: 'Rate limit exceeded',
status: 429,
};
const expected = `[API Error: Rate limit exceeded]\n${rateLimitMessage}`;
expect(parseAndFormatApiError(error)).toBe(expected);
const result = parseAndFormatApiError(error, AuthType.USE_VERTEX_AI);
expect(result).toContain('[API Error: Rate limit exceeded]');
expect(result).toContain(vertexMessage);
});
it('should handle an unknown error type', () => {

View File

@@ -4,10 +4,16 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { StructuredError } from '@gemini-cli/core';
import { AuthType, StructuredError } from '@gemini-cli/core';
const RATE_LIMIT_ERROR_MESSAGE =
const RATE_LIMIT_ERROR_MESSAGE_GOOGLE =
'\nPlease 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';
const RATE_LIMIT_ERROR_MESSAGE_USE_GEMINI =
'\nPlease wait and try again later. To increase your limits, request a quota increase through AI Studio, or switch to another /auth method';
const RATE_LIMIT_ERROR_MESSAGE_VERTEX =
'\nPlease wait and try again later. To increase your limits, request a quota increase through Vertex, or switch to another /auth method';
const RATE_LIMIT_ERROR_MESSAGE_DEFAULT =
'Your request has been rate limited. Please wait and try again later.';
export interface ApiError {
error: {
@@ -37,11 +43,28 @@ function isStructuredError(error: unknown): error is StructuredError {
);
}
export function parseAndFormatApiError(error: unknown): string {
function getRateLimitMessage(authType?: AuthType): string {
switch (authType) {
case AuthType.LOGIN_WITH_GOOGLE_PERSONAL:
case AuthType.LOGIN_WITH_GOOGLE_ENTERPRISE:
return RATE_LIMIT_ERROR_MESSAGE_GOOGLE;
case AuthType.USE_GEMINI:
return RATE_LIMIT_ERROR_MESSAGE_USE_GEMINI;
case AuthType.USE_VERTEX_AI:
return RATE_LIMIT_ERROR_MESSAGE_VERTEX;
default:
return RATE_LIMIT_ERROR_MESSAGE_DEFAULT;
}
}
export function parseAndFormatApiError(
error: unknown,
authType?: AuthType,
): string {
if (isStructuredError(error)) {
let text = `[API Error: ${error.message}]`;
if (error.status === 429) {
text += RATE_LIMIT_ERROR_MESSAGE;
text += getRateLimitMessage(authType);
}
return text;
}
@@ -70,7 +93,7 @@ export function parseAndFormatApiError(error: unknown): string {
}
let text = `[API Error: ${finalMessage} (Status: ${parsedError.error.status})]`;
if (parsedError.error.code === 429) {
text += RATE_LIMIT_ERROR_MESSAGE;
text += getRateLimitMessage(authType);
}
return text;
}