diff options
Diffstat (limited to 'packages/core/src/utils')
| -rw-r--r-- | packages/core/src/utils/quotaErrorDetection.ts | 17 | ||||
| -rw-r--r-- | packages/core/src/utils/safeJsonStringify.test.ts | 73 | ||||
| -rw-r--r-- | packages/core/src/utils/safeJsonStringify.ts | 32 |
3 files changed, 108 insertions, 14 deletions
diff --git a/packages/core/src/utils/quotaErrorDetection.ts b/packages/core/src/utils/quotaErrorDetection.ts index a8e87a5d..b07309cd 100644 --- a/packages/core/src/utils/quotaErrorDetection.ts +++ b/packages/core/src/utils/quotaErrorDetection.ts @@ -44,20 +44,9 @@ export function isProQuotaExceededError(error: unknown): boolean { // - "Quota exceeded for quota metric 'Gemini 2.5-preview Pro Requests'" // We use string methods instead of regex to avoid ReDoS vulnerabilities - const checkMessage = (message: string): boolean => { - console.log('[DEBUG] isProQuotaExceededError checking message:', message); - const result = - message.includes("Quota exceeded for quota metric 'Gemini") && - message.includes("Pro Requests'"); - console.log('[DEBUG] isProQuotaExceededError result:', result); - return result; - }; - - // Log the full error object to understand its structure - console.log( - '[DEBUG] isProQuotaExceededError - full error object:', - JSON.stringify(error, null, 2), - ); + const checkMessage = (message: string): boolean => + message.includes("Quota exceeded for quota metric 'Gemini") && + message.includes("Pro Requests'"); if (typeof error === 'string') { return checkMessage(error); diff --git a/packages/core/src/utils/safeJsonStringify.test.ts b/packages/core/src/utils/safeJsonStringify.test.ts new file mode 100644 index 00000000..9a38c048 --- /dev/null +++ b/packages/core/src/utils/safeJsonStringify.test.ts @@ -0,0 +1,73 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect } from 'vitest'; +import { safeJsonStringify } from './safeJsonStringify.js'; + +describe('safeJsonStringify', () => { + it('should stringify normal objects without issues', () => { + const obj = { name: 'test', value: 42 }; + const result = safeJsonStringify(obj); + expect(result).toBe('{"name":"test","value":42}'); + }); + + it('should handle circular references by replacing them with [Circular]', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const obj: any = { name: 'test' }; + obj.circular = obj; // Create circular reference + + const result = safeJsonStringify(obj); + expect(result).toBe('{"name":"test","circular":"[Circular]"}'); + }); + + it('should handle complex circular structures like HttpsProxyAgent', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const agent: any = { + sockets: {}, + options: { host: 'example.com' }, + }; + agent.sockets['example.com'] = [{ agent }]; + + const result = safeJsonStringify(agent); + expect(result).toContain('[Circular]'); + expect(result).toContain('example.com'); + }); + + it('should respect the space parameter for formatting', () => { + const obj = { name: 'test', value: 42 }; + const result = safeJsonStringify(obj, 2); + expect(result).toBe('{\n "name": "test",\n "value": 42\n}'); + }); + + it('should handle circular references with formatting', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const obj: any = { name: 'test' }; + obj.circular = obj; + + const result = safeJsonStringify(obj, 2); + expect(result).toBe('{\n "name": "test",\n "circular": "[Circular]"\n}'); + }); + + it('should handle arrays with circular references', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const arr: any[] = [{ id: 1 }]; + arr[0].parent = arr; // Create circular reference + + const result = safeJsonStringify(arr); + expect(result).toBe('[{"id":1,"parent":"[Circular]"}]'); + }); + + it('should handle null and undefined values', () => { + expect(safeJsonStringify(null)).toBe('null'); + expect(safeJsonStringify(undefined)).toBe(undefined); + }); + + it('should handle primitive values', () => { + expect(safeJsonStringify('test')).toBe('"test"'); + expect(safeJsonStringify(42)).toBe('42'); + expect(safeJsonStringify(true)).toBe('true'); + }); +}); diff --git a/packages/core/src/utils/safeJsonStringify.ts b/packages/core/src/utils/safeJsonStringify.ts new file mode 100644 index 00000000..f439bcea --- /dev/null +++ b/packages/core/src/utils/safeJsonStringify.ts @@ -0,0 +1,32 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * Safely stringifies an object to JSON, handling circular references by replacing them with [Circular]. + * + * @param obj - The object to stringify + * @param space - Optional space parameter for formatting (defaults to no formatting) + * @returns JSON string with circular references replaced by [Circular] + */ +export function safeJsonStringify( + obj: unknown, + space?: string | number, +): string { + const seen = new WeakSet(); + return JSON.stringify( + obj, + (key, value) => { + if (typeof value === 'object' && value !== null) { + if (seen.has(value)) { + return '[Circular]'; + } + seen.add(value); + } + return value; + }, + space, + ); +} |
