mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-14 23:31:13 -07:00
fix(core): improve error type extraction for telemetry (#19565)
Co-authored-by: Yuna Seol <yunaseol@google.com>
This commit is contained in:
@@ -12,6 +12,14 @@ import {
|
||||
BadRequestError,
|
||||
ForbiddenError,
|
||||
getErrorMessage,
|
||||
getErrorType,
|
||||
FatalAuthenticationError,
|
||||
FatalCancellationError,
|
||||
FatalInputError,
|
||||
FatalSandboxError,
|
||||
FatalConfigError,
|
||||
FatalTurnLimitedError,
|
||||
FatalToolExecutionError,
|
||||
} from './errors.js';
|
||||
|
||||
describe('getErrorMessage', () => {
|
||||
@@ -201,3 +209,44 @@ describe('toFriendlyError', () => {
|
||||
expect(toFriendlyError(error)).toBe(error);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getErrorType', () => {
|
||||
it('should return error name for standard errors', () => {
|
||||
expect(getErrorType(new Error('test'))).toBe('Error');
|
||||
expect(getErrorType(new TypeError('test'))).toBe('TypeError');
|
||||
expect(getErrorType(new SyntaxError('test'))).toBe('SyntaxError');
|
||||
});
|
||||
|
||||
it('should return constructor name for custom errors', () => {
|
||||
expect(getErrorType(new FatalAuthenticationError('test'))).toBe(
|
||||
'FatalAuthenticationError',
|
||||
);
|
||||
expect(getErrorType(new FatalInputError('test'))).toBe('FatalInputError');
|
||||
expect(getErrorType(new FatalSandboxError('test'))).toBe(
|
||||
'FatalSandboxError',
|
||||
);
|
||||
expect(getErrorType(new FatalConfigError('test'))).toBe('FatalConfigError');
|
||||
expect(getErrorType(new FatalTurnLimitedError('test'))).toBe(
|
||||
'FatalTurnLimitedError',
|
||||
);
|
||||
expect(getErrorType(new FatalToolExecutionError('test'))).toBe(
|
||||
'FatalToolExecutionError',
|
||||
);
|
||||
expect(getErrorType(new FatalCancellationError('test'))).toBe(
|
||||
'FatalCancellationError',
|
||||
);
|
||||
expect(getErrorType(new ForbiddenError('test'))).toBe('ForbiddenError');
|
||||
expect(getErrorType(new UnauthorizedError('test'))).toBe(
|
||||
'UnauthorizedError',
|
||||
);
|
||||
expect(getErrorType(new BadRequestError('test'))).toBe('BadRequestError');
|
||||
});
|
||||
|
||||
it('should return "unknown" for non-Error objects', () => {
|
||||
expect(getErrorType('string error')).toBe('unknown');
|
||||
expect(getErrorType(123)).toBe('unknown');
|
||||
expect(getErrorType({})).toBe('unknown');
|
||||
expect(getErrorType(null)).toBe('unknown');
|
||||
expect(getErrorType(undefined)).toBe('unknown');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -26,6 +26,15 @@ export function getErrorMessage(error: unknown): string {
|
||||
}
|
||||
}
|
||||
|
||||
export function getErrorType(error: unknown): string {
|
||||
if (!(error instanceof Error)) return 'unknown';
|
||||
|
||||
// Return constructor name if the generic 'Error' name is used (for custom errors)
|
||||
return error.name === 'Error'
|
||||
? (error.constructor?.name ?? 'Error')
|
||||
: error.name;
|
||||
}
|
||||
|
||||
export class FatalError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
|
||||
Reference in New Issue
Block a user