mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-22 11:04:42 -07:00
refactor: localize ACP error parsing logic to cli package (#18193)
This commit is contained in:
@@ -19,37 +19,6 @@ describe('getErrorMessage', () => {
|
||||
expect(getErrorMessage(new Error('plain error'))).toBe('plain error');
|
||||
});
|
||||
|
||||
it('should parse simple JSON error response', () => {
|
||||
const json = JSON.stringify({ error: { message: 'json error' } });
|
||||
expect(getErrorMessage(new Error(json))).toBe('json error');
|
||||
});
|
||||
|
||||
it('should parse double-encoded JSON error response', () => {
|
||||
const innerJson = JSON.stringify({ error: { message: 'nested error' } });
|
||||
const outerJson = JSON.stringify({ error: { message: innerJson } });
|
||||
expect(getErrorMessage(new Error(outerJson))).toBe('nested error');
|
||||
});
|
||||
|
||||
it('should parse array-style JSON error response', () => {
|
||||
const json = JSON.stringify([{ error: { message: 'array error' } }]);
|
||||
expect(getErrorMessage(new Error(json))).toBe('array error');
|
||||
});
|
||||
|
||||
it('should parse JSON with top-level message field', () => {
|
||||
const json = JSON.stringify({ message: 'top-level message' });
|
||||
expect(getErrorMessage(new Error(json))).toBe('top-level message');
|
||||
});
|
||||
|
||||
it('should handle JSON with trailing newline', () => {
|
||||
const json = JSON.stringify({ error: { message: 'newline error' } }) + '\n';
|
||||
expect(getErrorMessage(new Error(json))).toBe('newline error');
|
||||
});
|
||||
|
||||
it('should return original message if JSON parsing fails', () => {
|
||||
const invalidJson = '{ not-json }';
|
||||
expect(getErrorMessage(new Error(invalidJson))).toBe(invalidJson);
|
||||
});
|
||||
|
||||
it('should handle non-Error inputs', () => {
|
||||
expect(getErrorMessage('string error')).toBe('string error');
|
||||
expect(getErrorMessage(123)).toBe('123');
|
||||
|
||||
@@ -16,40 +16,11 @@ export function isNodeError(error: unknown): error is NodeJS.ErrnoException {
|
||||
|
||||
export function getErrorMessage(error: unknown): string {
|
||||
const friendlyError = toFriendlyError(error);
|
||||
return extractMessage(friendlyError);
|
||||
}
|
||||
|
||||
function extractMessage(input: unknown): string {
|
||||
if (input instanceof Error) {
|
||||
return extractMessage(input.message);
|
||||
if (friendlyError instanceof Error) {
|
||||
return friendlyError.message;
|
||||
}
|
||||
|
||||
if (typeof input === 'string') {
|
||||
const trimmed = input.trim();
|
||||
// Attempt to parse JSON error responses (common in Google API errors)
|
||||
if (
|
||||
(trimmed.startsWith('{') && trimmed.endsWith('}')) ||
|
||||
(trimmed.startsWith('[') && trimmed.endsWith(']'))
|
||||
) {
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed);
|
||||
const next =
|
||||
parsed?.error?.message ||
|
||||
parsed?.[0]?.error?.message ||
|
||||
parsed?.message;
|
||||
|
||||
if (next && next !== input) {
|
||||
return extractMessage(next);
|
||||
}
|
||||
} catch {
|
||||
// Fall back to original string if parsing fails
|
||||
}
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
try {
|
||||
return String(input);
|
||||
return String(friendlyError);
|
||||
} catch {
|
||||
return 'Failed to get error details';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user