diff --git a/index.js b/index.js index 9ca90c0..8f286e4 100644 --- a/index.js +++ b/index.js @@ -110,7 +110,11 @@ const destroyCircular = ({ } if (!value || typeof value !== 'object') { - to[key] = value; + // Gracefully handle non-configurable errors like `DOMException`. + try { + to[key] = value; + } catch {} + continue; } @@ -159,7 +163,8 @@ export function serializeError(value, options = {}) { // People sometimes throw things besides Error objects… if (typeof value === 'function') { // `JSON.stringify()` discards functions. We do too, unless a function is thrown directly. - return `[Function: ${value.name ?? 'anonymous'}]`; + // We intentionally use `||` because `.name` is an empty string for anonymous functions. + return `[Function: ${value.name || 'anonymous'}]`; } return value; diff --git a/test.js b/test.js index ca0d741..45ced34 100644 --- a/test.js +++ b/test.js @@ -520,3 +520,10 @@ test('should serialize custom non-extensible error with custom `.toJSON` propert t.not(stack, undefined); }); + +if ('DOMException' in globalThis) { + test('should serialize DOMException', t => { + const serialized = serializeError(new DOMException('x')); + t.is(serialized.message, 'x'); + }); +}