Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@ import type { GenerateContentParameters } from '@google/genai';
import { EnhancedErrorHandler } from './errorHandler.js';
import type { RequestContext } from './types.js';

const debugLoggerSpy = vi.hoisted(() => ({
error: vi.fn(),
}));

vi.mock('../../utils/debugLogger.js', () => ({
createDebugLogger: () => ({
error: debugLoggerSpy.error,
}),
}));

describe('EnhancedErrorHandler', () => {
const fixedNow = 10_000;
let errorHandler: EnhancedErrorHandler;
let mockContext: RequestContext;
let mockRequest: GenerateContentParameters;

beforeEach(() => {
debugLoggerSpy.error.mockReset();
vi.spyOn(Date, 'now').mockReturnValue(fixedNow);
mockContext = {
model: 'test-model',
Expand Down Expand Up @@ -59,6 +70,37 @@ describe('EnhancedErrorHandler', () => {
}).toThrow(originalError);
});

it('logs structured API diagnostics without request contents', () => {
const apiError = Object.assign(
new Error(
'event:error\n:HTTP_STATUS/429\ndata:{"request_id":"req-123","code":"Throttling.AllocationQuota","message":"Allocated quota exceeded"}',
),
{ type: 'rate_limit_error' },
);

expect(() => {
errorHandler.handle(apiError, mockContext, mockRequest);
}).toThrow(apiError);

expect(debugLoggerSpy.error).toHaveBeenCalledWith(
'OpenAI API Error:',
expect.any(String),
{
durationMs: 5000,
errorType: 'rate_limit_error',
model: 'test-model',
providerCode: 'Throttling.AllocationQuota',
providerMessage: 'Allocated quota exceeded',
requestId: 'req-123',
statusCode: 429,
transport: 'sse',
},
);
expect(JSON.stringify(debugLoggerSpy.error.mock.calls[0])).not.toContain(
'test prompt',
);
});

it('should throw enhanced error message for timeout errors', () => {
const timeoutError = new Error('Request timeout');

Expand Down
64 changes: 63 additions & 1 deletion packages/core/src/core/openaiContentGenerator/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@

import type { GenerateContentParameters } from '@google/genai';
import { createDebugLogger } from '../../utils/debugLogger.js';
import { getErrorStatus, getErrorType } from '../../utils/errors.js';
import { getRateLimitErrorDetails } from '../../utils/rateLimit.js';
import { redactProxyError } from '../../utils/runtimeFetchOptions.js';
import type { ErrorHandler, RequestContext } from './types.js';

const debugLogger = createDebugLogger('OPENAI_ERROR');
export type { ErrorHandler } from './types.js';

interface ApiErrorDiagnostics {
model: string;
durationMs: number;
errorType: string;
statusCode?: number;
providerCode?: string;
providerMessage?: string;
requestId?: string;
transport?: 'http' | 'sse' | 'unknown';
}

export class EnhancedErrorHandler implements ErrorHandler {
constructor(
private shouldSuppressLogging: (
Expand All @@ -35,7 +48,11 @@ export class EnhancedErrorHandler implements ErrorHandler {

// Allow subclasses to suppress error logging for specific scenarios
if (!this.shouldSuppressErrorLogging(redactedError, request)) {
debugLogger.error('OpenAI API Error:', errorMessage);
debugLogger.error(
'OpenAI API Error:',
errorMessage,
this.buildDiagnostics(redactedError, context),
);
}

// Provide helpful timeout-specific error message
Expand Down Expand Up @@ -98,6 +115,51 @@ export class EnhancedErrorHandler implements ErrorHandler {
return error instanceof Error ? error.message : String(error);
}

private buildDiagnostics(
error: unknown,
context: RequestContext,
): ApiErrorDiagnostics {
const details = getRateLimitErrorDetails(error);
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
const requestId = this.getRequestId(error) ?? details.requestId;
return {
model: context.model,
durationMs: Date.now() - context.startTime,
errorType: getErrorType(error),
...(getErrorStatus(error) !== undefined
? { statusCode: getErrorStatus(error) }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] getErrorStatus(error) is called twice in this conditional spread (once for the guard, once for the value) and a third time inside getRateLimitErrorDetails. Since it's a pure function, consider computing once and reusing:

Suggested change
? { statusCode: getErrorStatus(error) }
...(details.statusCode !== undefined
? { statusCode: details.statusCode }
: {}),

Or compute it once before the return:

const statusCode = getErrorStatus(error);

— qwen3.7-max via Qwen Code /review

: {}),
...(details.providerCode !== undefined
? { providerCode: details.providerCode }
: {}),
...(details.providerMessage !== undefined
? { providerMessage: details.providerMessage }
: {}),
...(requestId !== undefined ? { requestId } : {}),
...(details.transport !== 'unknown'
? { transport: details.transport }
: {}),
};
}

private getRequestId(error: unknown): string | undefined {
if (!error || typeof error !== 'object') return undefined;
const source = error as {
requestID?: unknown;
request_id?: unknown;
response_id?: unknown;
};
for (const value of [
source.requestID,
source.request_id,
source.response_id,
]) {
if (typeof value === 'string' && value) {
return value;
}
}
return undefined;
}

private getTimeoutTroubleshootingTips(): string {
const tips = [
'- Reduce input length or complexity',
Expand Down
20 changes: 16 additions & 4 deletions packages/core/src/utils/debugLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,20 +300,32 @@ describe('debugLogger', () => {

describe('latest debug log symlink', () => {
const expectedLatestPath = path.join(Storage.getGlobalDebugDir(), 'latest');
const uuidSession: DebugLogSession = {
getSessionId: () => '92ec0176-d354-4147-848b-5cd2d80609c4',
};

it('creates a symlink to the current session log file', async () => {
resetDebugLoggingState();
setDebugLogSession(mockSession);
setDebugLogSession(uuidSession);

await vi.runAllTimersAsync();

expect(fs.unlink).toHaveBeenCalledWith(expectedLatestPath);
expect(fs.symlink).toHaveBeenCalledWith(
'test-session-123.txt',
'92ec0176-d354-4147-848b-5cd2d80609c4.txt',
expectedLatestPath,
);
});

it('does not point latest at non-session debug logs', async () => {
resetDebugLoggingState();
setDebugLogSession({ getSessionId: () => 'log-to-span-sink-test' });

await vi.runAllTimersAsync();

expect(fs.symlink).not.toHaveBeenCalled();
});

it('does not create symlink when session is cleared', async () => {
vi.clearAllMocks();
resetDebugLoggingState();
Expand All @@ -328,7 +340,7 @@ describe('debugLogger', () => {
resetDebugLoggingState();
vi.mocked(fs.symlink).mockRejectedValueOnce(new Error('EPERM'));

setDebugLogSession(mockSession);
setDebugLogSession(uuidSession);

await vi.runAllTimersAsync();

Expand All @@ -339,7 +351,7 @@ describe('debugLogger', () => {
process.env['QWEN_DEBUG_LOG_FILE'] = '0';
vi.clearAllMocks();
resetDebugLoggingState();
setDebugLogSession(mockSession);
setDebugLogSession(uuidSession);

await vi.runAllTimersAsync();

Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/utils/debugLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,16 @@ export function resetDebugLoggingState(): void {
}

const DEBUG_LATEST_ALIAS = 'latest';
const SESSION_ID_PATTERN =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;

function updateLatestDebugLogAlias(sessionId: string): void {
if (!isDebugLogFileEnabled()) {
return;
}
if (!SESSION_ID_PATTERN.test(sessionId)) {
return;
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
}

const aliasPath = path.join(Storage.getGlobalDebugDir(), DEBUG_LATEST_ALIAS);
const targetPath = Storage.getDebugLogPath(sessionId);
Expand Down
42 changes: 22 additions & 20 deletions packages/core/src/utils/errorReporting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ describe('reportError', () => {
);
});

it('summarizes context instead of logging raw prompt contents', async () => {
const error = new Error('API failed');
const baseMessage = 'Error generating text content via API.';
const context = [
{
role: 'user',
parts: [{ text: 'secret prompt that should not be in debug logs' }],
},
];

await reportError(error, baseMessage, context, 'generateText-api');

const report = String(debugLoggerSpy.error.mock.calls[0]?.[1]);
expect(report).not.toContain('secret prompt');
expect(report).not.toContain('"context"');
expect(report).toContain('"contextSummary"');
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
expect(report).toContain('"kind": "array"');
expect(report).toContain('"itemCount": 1');
});

it('should handle errors that are plain objects with a message property', async () => {
const error = { message: 'Test plain object error' };
const baseMessage = 'Another error.';
Expand Down Expand Up @@ -78,36 +98,18 @@ describe('reportError', () => {
);
});

it('should handle stringification failure of report content (e.g. BigInt in context)', async () => {
it('should not stringify raw context when context contains unsupported values', async () => {
const error = new Error('Main error');
error.stack = 'Main stack';
const baseMessage = 'Failed operation with BigInt.';
const context = { a: BigInt(1) }; // BigInt cannot be stringified by JSON.stringify

// Simulate JSON.stringify throwing an error for the full report
const originalJsonStringify = JSON.stringify;
let callCount = 0;
vi.spyOn(JSON, 'stringify').mockImplementation((value, replacer, space) => {
callCount++;
if (callCount === 1) {
// First call is for the full report content
throw new TypeError('Do not know how to serialize a BigInt');
}
// Subsequent calls (for minimal report) should succeed
return originalJsonStringify(value, replacer, space);
});

await expect(
reportError(error, baseMessage, context, 'bigint-fail'),
).resolves.not.toThrow();
expect(debugLoggerSpy.error).toHaveBeenCalledWith(
`${baseMessage} [bigint-fail] Could not stringify report content (likely due to context):`,
expect.any(TypeError),
error,
);
expect(debugLoggerSpy.error).toHaveBeenCalledWith(
`${baseMessage} [bigint-fail]`,
expect.any(String),
expect.stringContaining('"contextSummary"'),
);
});

Expand Down
22 changes: 20 additions & 2 deletions packages/core/src/utils/errorReporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,28 @@ const debugLogger = createDebugLogger('ERROR_REPORT');

interface ErrorReportData {
error: { message: string; stack?: string } | { message: string };
context?: unknown;
contextSummary?: ContextSummary;
additionalInfo?: Record<string, unknown>;
}

type ContextSummary =
| { kind: 'array'; itemCount: number }
| { kind: 'object'; keys: string[] }
| { kind: string };

function summarizeContext(context: unknown): ContextSummary {
if (Array.isArray(context)) {
return { kind: 'array', itemCount: context.length };
}
if (context && typeof context === 'object') {
return {
kind: 'object',
keys: Object.keys(context).slice(0, 20),
};
}
return { kind: typeof context };
}

/**
* Generates an error report and writes it to the debug log.
* @param error The error object.
Expand Down Expand Up @@ -46,7 +64,7 @@ export async function reportError(
const reportContent: ErrorReportData = { error: errorToReport };

if (context) {
reportContent.context = context;
reportContent.contextSummary = summarizeContext(context);
}

const reportLabel = `${baseMessage} [${type}]`;
Expand Down
Loading