diff --git a/packages/cli/src/utils/errors.test.ts b/packages/cli/src/utils/errors.test.ts index 2515fe402..63a60bf65 100644 --- a/packages/cli/src/utils/errors.test.ts +++ b/packages/cli/src/utils/errors.test.ts @@ -125,6 +125,15 @@ describe('errors', () => { const obj = { message: 123 }; expect(getErrorMessage(obj)).toBe('{"message":123}'); }); + + it('should fallback to String() when toJSON returns undefined', () => { + const obj = { + toJSON() { + return undefined; + }, + }; + expect(getErrorMessage(obj)).toBe('[object Object]'); + }); }); describe('handleError', () => { diff --git a/packages/cli/src/utils/errors.ts b/packages/cli/src/utils/errors.ts index 27966df4f..676418708 100644 --- a/packages/cli/src/utils/errors.ts +++ b/packages/cli/src/utils/errors.ts @@ -31,7 +31,9 @@ export function getErrorMessage(error: unknown): string { // Handle plain objects by stringifying them if (error !== null && typeof error === 'object') { try { - return JSON.stringify(error); + const stringified = JSON.stringify(error); + // JSON.stringify can return undefined for objects with toJSON() returning undefined + return stringified ?? String(error); } catch { // If JSON.stringify fails (circular reference, etc.), fall back to String return String(error);