util: throw toJSON errors when formatting %j · nodejs/node@455e6f1

Original file line numberDiff line numberDiff line change

@@ -86,6 +86,16 @@ assert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j');

8686

assert.strictEqual(util.format('%j', o), '[Circular]');

8787

}

8888
89+

{

90+

const o = {

91+

toJSON() {

92+

throw new Error('Not a circular object but still not serializable');

93+

}

94+

};

95+

assert.throws(() => util.format('%j', o),

96+

/^Error: Not a circular object but still not serializable$/);

97+

}

98+
8999

// Errors

90100

const err = new Error('foo');

91101

assert.strictEqual(util.format(err), err.stack);