JacobHawley's picture
download
raw
59.6 kB
diff --git a/src/error-stack.test.ts b/src/error-stack.test.ts new file mode 100644 index 0000000..d0583b5 --- /dev/null +++ b/src/error-stack.test.ts @@ -0,0 +1,1522 @@ +import { describe, it, expect } from 'vitest'; +import SuperJSON from './index.js'; + +describe('Error Stack Serialization – Core', () => { + describe('Legacy behavior preserved when no errorStack option', () => { + it('serializes error message and name', () => { + const sj = new SuperJSON(); + const e = new Error('hello'); + const out = sj.parse<Error>(sj.stringify(e)); + expect(out).toBeInstanceOf(Error); + expect(out.message).toBe('hello'); + }); + + it('preserves stack when allowErrorProps is used (legacy)', () => { + const sj = new SuperJSON(); + sj.allowErrorProps('stack'); + const e = new Error('test'); + const stack = e.stack!; + const out = sj.parse<Error>(sj.stringify(e)); + expect(out.stack).toBe(stack); + }); + + it('does not serialize stack when not in allowedErrorProps', () => { + const sj = new SuperJSON(); + const e = new Error('test'); + const { json } = sj.serialize(e); + expect((json as any).stack).toBeUndefined(); + }); + + it('preserves cause in legacy mode', () => { + const sj = new SuperJSON(); + const cause = new Error('root'); + const e = new Error('top', { cause }); + const out = sj.parse<Error>(sj.stringify(e)); + expect((out as any).cause).toBeInstanceOf(Error); + expect((out as any).cause.message).toBe('root'); + }); + + it('round-trips custom name', () => { + const sj = new SuperJSON(); + const e = new Error('custom'); + e.name = 'DBError'; + const out = sj.parse<Error>(sj.stringify(e)); + expect(out.name).toBe('DBError'); + }); + + it('uses Error annotation when no errorStack option', () => { + const sj = new SuperJSON(); + const { meta } = sj.serialize(new Error('x')); + // annotation must be 'Error', never 'Error/stack' or 'Error/frames' + const raw = JSON.stringify(meta?.values); + expect(raw).toContain('"Error"'); + expect(raw).not.toContain('Error/stack'); + expect(raw).not.toContain('Error/frames'); + }); + }); + + describe('mode=off behavior', () => { + it('mode=off suppresses stack even if allowErrorProps contains stack', () => { + const sj = new SuperJSON({ errorStack: { mode: 'off' } }); + sj.allowErrorProps('stack'); + const e = new Error('test'); + e.stack = 'Error: test\nat app.ts:1:1'; + const { json } = sj.serialize(e); + expect((json as any).stack).toBeUndefined(); + }); + + it('mode=off uses "Error" annotation (not Error/off)', () => { + const sj = new SuperJSON({ errorStack: { mode: 'off' } }); + const { meta } = sj.serialize(new Error('x')); + const raw = JSON.stringify(meta?.values); + expect(raw).toContain('"Error"'); + expect(raw).not.toContain('Error/off'); + expect(raw).not.toContain('Error/stack'); + expect(raw).not.toContain('Error/frames'); + }); + + it('mode=off still preserves name and message', () => { + const sj = new SuperJSON({ errorStack: { mode: 'off' } }); + const e = new TypeError('bad type'); + const out = sj.parse<Error>(sj.stringify(e)); + expect(out.name).toBe('TypeError'); + expect(out.message).toBe('bad type'); + }); + }); + + describe('mode=string annotations', () => { + it('mode=string uses "Error/stack" annotation', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = 'Error: x\nat app.ts:1:1'; + const { meta } = sj.serialize(e); + const raw = JSON.stringify(meta?.values); + expect(raw).toContain('Error/stack'); + }); + + it('mode=string annotation is exactly "Error/stack" not "Error:stack"', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + const { meta } = sj.serialize(e); + const raw = JSON.stringify(meta?.values); + expect(raw).toContain('"Error/stack"'); + expect(raw).not.toContain('"Error:stack"'); + expect(raw).not.toContain('"ErrorStack"'); + }); + + it('mode=string round-trips stack as string', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const e = new Error('str'); + e.stack = 'Error: str\nat app.ts:5:1'; + const out = sj.parse<Error>(sj.stringify(e)); + expect(typeof out.stack).toBe('string'); + expect(out.stack).toContain('Error: str'); + }); + + it('mode=string: allowErrorProps("stackFrames") has no effect', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stackFrames'); + const e = new Error('x'); + e.stack = 'Error: x\nat app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stackFrames).toBeUndefined(); + }); + + it('mode=string does not produce stackFrames even if stack allowed', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack', 'stackFrames'); + const e = new Error('x'); + const { json } = sj.serialize(e); + expect((json as any).stackFrames).toBeUndefined(); + }); + }); + + describe('mode=frames annotations', () => { + it('mode=frames uses "Error/frames" annotation', () => { + const sj = new SuperJSON({ errorStack: { mode: 'frames' } }); + sj.allowErrorProps('stackFrames'); + const e = new Error('x'); + e.stack = 'Error: x\nat app.ts:1:1'; + const { meta } = sj.serialize(e); + const raw = JSON.stringify(meta?.values); + expect(raw).toContain('Error/frames'); + }); + + it('mode=frames annotation is exactly "Error/frames"', () => { + const sj = new SuperJSON({ errorStack: { mode: 'frames' } }); + sj.allowErrorProps('stackFrames'); + const e = new Error('x'); + const { meta } = sj.serialize(e); + const raw = JSON.stringify(meta?.values); + expect(raw).toContain('"Error/frames"'); + expect(raw).not.toContain('"Error:frames"'); + expect(raw).not.toContain('"ErrorFrames"'); + }); + + it('mode=frames round-trips stackFrames array', () => { + const sj = new SuperJSON({ errorStack: { mode: 'frames' } }); + sj.allowErrorProps('stackFrames'); + const e = new Error('f'); + e.stack = 'Error: f\nat app.ts:10:1'; + const out = sj.parse<any>(sj.stringify(e)); + expect(Array.isArray(out.stackFrames)).toBe(true); + expect(out.stackFrames[0].raw).toContain('Error: f'); + }); + + it('mode=frames: allowErrorProps("stack") without stackFrames produces no stackFrames', () => { + const sj = new SuperJSON({ errorStack: { mode: 'frames' } }); + sj.allowErrorProps('stack'); // only 'stack', NOT 'stackFrames' + const e = new Error('x'); + e.stack = 'Error: x\nat /app/app.ts:1:1'; + const { json } = sj.serialize(e); + // mode=frames only produces stackFrames, but stackFrames is not in allowedErrorProps + expect((json as any).stackFrames).toBeUndefined(); + }); + + it('mode=frames does not produce stack string', () => { + const sj = new SuperJSON({ errorStack: { mode: 'frames' } }); + sj.allowErrorProps('stack', 'stackFrames'); + const e = new Error('x'); + e.stack = 'Error: x\nat app.ts:1:1'; + const { json } = sj.serialize(e); + expect(typeof (json as any).stack).not.toBe('string'); + }); + }); +}); + +describe('Error Stack – classFilter', () => { + it('classFilter: empty array applies to ALL errors', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string', classFilter: [] } }); + sj.allowErrorProps('stack'); + const e = new RangeError('out of range'); + e.stack = 'RangeError: out of range\nat app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(e)); + expect(typeof out.stack).toBe('string'); + }); + + it('classFilter: undefined applies to ALL errors', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const e = new TypeError('bad'); + e.stack = 'TypeError: bad\nat app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(e)); + expect(typeof out.stack).toBe('string'); + }); + + it('classFilter: non-empty list applies ONLY to matched .name', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', classFilter: ['TypeError'] }, + }); + sj.allowErrorProps('stack'); + const te = new TypeError('bad type'); + te.stack = 'TypeError: bad type\nat app.ts:1:1'; + const re = new RangeError('bad range'); + re.stack = 'RangeError: bad range\nat app.ts:2:2'; + + const outTE = sj.parse<any>(sj.stringify(te)); + const outRE = sj.parse<any>(sj.stringify(re)); + + // TypeError matches filter → mode=string applied → has Error/stack annotation + const { meta: metaTE } = sj.serialize(te); + expect(JSON.stringify(metaTE?.values)).toContain('Error/stack'); + + // RangeError does NOT match → legacy mode → has Error annotation + const { meta: metaRE } = sj.serialize(re); + expect(JSON.stringify(metaRE?.values)).toContain('"Error"'); + expect(JSON.stringify(metaRE?.values)).not.toContain('Error/stack'); + }); + + it('classFilter: matches by error.name not error.constructor.name', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', classFilter: ['DBError'] }, + }); + sj.allowErrorProps('stack'); + + const e = new Error('db problem'); + e.name = 'DBError'; // custom name on a base Error instance + e.stack = 'DBError: db problem\nat db.ts:5:1'; + + const { meta } = sj.serialize(e); + // name is 'DBError' which is in classFilter → should get Error/stack annotation + expect(JSON.stringify(meta?.values)).toContain('Error/stack'); + }); + + it('classFilter: Error with non-matching name uses legacy annotation', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', classFilter: ['DBError'] }, + }); + sj.allowErrorProps('stack'); + + const e = new Error('plain error'); + // name is 'Error' not 'DBError' + const { meta } = sj.serialize(e); + expect(JSON.stringify(meta?.values)).toContain('"Error"'); + expect(JSON.stringify(meta?.values)).not.toContain('Error/stack'); + }); + + it('classFilter: non-matching error still serializes name and message', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', classFilter: ['SpecialError'] }, + }); + const e = new TypeError('type issue'); + const out = sj.parse<Error>(sj.stringify(e)); + expect(out.name).toBe('TypeError'); + expect(out.message).toBe('type issue'); + }); +}); + +describe('Error Stack – sanitizeMessage', () => { + it('sanitizeMessage replaces https URLs with [redacted]', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', sanitizeMessage: true }, + }); + sj.allowErrorProps('stack'); + const e = new Error('Failed to fetch https://api.example.com/data'); + const { json } = sj.serialize(e); + expect((json as any).message).toBe('Failed to fetch [redacted]'); + }); + + it('sanitizeMessage replaces http URLs with [redacted]', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', sanitizeMessage: true }, + }); + sj.allowErrorProps('stack'); + const e = new Error('Connecting to http://internal.corp/api'); + const { json } = sj.serialize(e); + expect((json as any).message).toBe('Connecting to [redacted]'); + }); + + it('sanitizeMessage replaces email addresses with [redacted]', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', sanitizeMessage: true }, + }); + sj.allowErrorProps('stack'); + const e = new Error('User user@example.com not found'); + const { json } = sj.serialize(e); + expect((json as any).message).toBe('User [redacted] not found'); + }); + + it('sanitizeMessage replaces IPv4 addresses with [redacted]', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', sanitizeMessage: true }, + }); + sj.allowErrorProps('stack'); + const e = new Error('Connection refused: 192.168.1.100'); + const { json } = sj.serialize(e); + expect((json as any).message).toBe('Connection refused: [redacted]'); + }); + + it('sanitizeMessage replaces multiple patterns in one message', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', sanitizeMessage: true }, + }); + sj.allowErrorProps('stack'); + const e = new Error( + 'Error at https://api.com for user@test.com from 10.0.0.1' + ); + const { json } = sj.serialize(e); + const msg = (json as any).message as string; + expect(msg).not.toContain('https://api.com'); + expect(msg).not.toContain('user@test.com'); + expect(msg).not.toContain('10.0.0.1'); + expect(msg).toContain('[redacted]'); + }); + + it('sanitizeMessage=false preserves original message', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', sanitizeMessage: false }, + }); + sj.allowErrorProps('stack'); + const e = new Error('Error at https://api.example.com'); + const { json } = sj.serialize(e); + expect((json as any).message).toBe('Error at https://api.example.com'); + }); + + it('sanitizeMessage without errorStack does not sanitize', () => { + const sj = new SuperJSON(); + const e = new Error('User user@test.com failed'); + const { json } = sj.serialize(e); + expect((json as any).message).toBe('User user@test.com failed'); + }); + + it('sanitizeMessage replacement is exactly [redacted] not *** or REDACTED', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', sanitizeMessage: true }, + }); + sj.allowErrorProps('stack'); + const e = new Error('hit https://example.com'); + const { json } = sj.serialize(e); + expect((json as any).message).toBe('hit [redacted]'); + expect((json as any).message).not.toContain('***'); + expect((json as any).message).not.toContain('REDACTED'); + }); + + it('sanitizeMessage also redacts included cause messages', () => { + const sj = new SuperJSON({ + errorStack: { + mode: 'string', + sanitizeMessage: true, + includeCauses: 'direct', + }, + }); + const err = new Error('top https://api.example.com', { + cause: new Error('inner admin@example.com'), + }); + const { json } = sj.serialize(err); + expect((json as any).message).toBe('top [redacted]'); + expect((json as any).cause.message).toBe('inner [redacted]'); + }); + + it('sanitizeMessage also redacts cause messages in frames mode', () => { + const sj = new SuperJSON({ + errorStack: { + mode: 'frames', + sanitizeMessage: true, + includeCauses: 'direct', + }, + }); + sj.allowErrorProps('stackFrames'); + const err = new Error('top https://api.example.com', { + cause: new Error('inner admin@example.com'), + }); + const { json } = sj.serialize(err); + expect((json as any).message).toBe('top [redacted]'); + expect((json as any).cause.message).toBe('inner [redacted]'); + }); +}); + +describe('Error Stack – registerErrorStackProcessor', () => { + it('processor is called after serialization', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + sj.registerErrorStackProcessor('Error', serialized => ({ + ...serialized, + processed: true, + })); + const e = new Error('test'); + const { json } = sj.serialize(e); + expect((json as any).processed).toBe(true); + }); + + it('processor receives serialized plain object (not original Error)', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + let capturedArg: any = null; + sj.registerErrorStackProcessor('Error', serialized => { + capturedArg = serialized; + return serialized; + }); + const e = new Error('capture me'); + sj.serialize(e); + expect(capturedArg).not.toBeInstanceOf(Error); + expect(typeof capturedArg).toBe('object'); + expect(capturedArg.message).toBe('capture me'); + }); + + it('processor matched by error.name', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + let called = false; + sj.registerErrorStackProcessor('RangeError', () => { + called = true; + return { name: 'RangeError', message: 'modified' }; + }); + const e = new RangeError('original'); + sj.serialize(e); + expect(called).toBe(true); + }); + + it('processor NOT called for different error name', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + let called = false; + sj.registerErrorStackProcessor('TypeError', () => { + called = true; + return { name: 'TypeError', message: 'modified' }; + }); + const e = new RangeError('range'); + sj.serialize(e); + expect(called).toBe(false); + }); + + it('processor return value is used in final output', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + sj.registerErrorStackProcessor('Error', _serialized => ({ + name: 'Error', + message: 'overridden by processor', + })); + const e = new Error('original message'); + const out = sj.parse<any>(sj.stringify(e)); + expect(out.message).toBe('overridden by processor'); + }); + + it('processor runs AFTER stripInternalFrames', () => { + const sj = new SuperJSON({ + errorStack: { + mode: 'string', + stripInternalFrames: 'node', + }, + }); + sj.allowErrorProps('stack'); + let capturedStack = ''; + sj.registerErrorStackProcessor('Error', serialized => { + capturedStack = (serialized as any).stack ?? ''; + return serialized; + }); + const e = new Error('x'); + e.stack = 'Error: x\nat foo.ts:1:1\nnode:internal/process:1:1'; + sj.serialize(e); + expect(capturedStack).not.toContain('node:internal'); + }); + + it('processor runs AFTER sanitizeMessage', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', sanitizeMessage: true }, + }); + sj.allowErrorProps('stack'); + let capturedMsg = ''; + sj.registerErrorStackProcessor('Error', serialized => { + capturedMsg = (serialized as any).message ?? ''; + return serialized; + }); + const e = new Error('fetched from https://api.example.com'); + sj.serialize(e); + expect(capturedMsg).not.toContain('https://api.example.com'); + expect(capturedMsg).toContain('[redacted]'); + }); + + it('registerErrorStackProcessor is available on instance', () => { + const sj = new SuperJSON(); + expect(typeof sj.registerErrorStackProcessor).toBe('function'); + }); +}); + +describe('Error Stack – option normalization edge cases', () => { + it('invalid mode string falls back to mode=off', () => { + const sj = new SuperJSON({ errorStack: { mode: 'invalid' as any } }); + sj.allowErrorProps('stack'); + const e = new Error('test'); + e.stack = 'Error: test\nat app.ts:1:1'; + const { json } = sj.serialize(e); + expect((json as any).stack).toBeUndefined(); + }); + + it('invalid maxStackLines (0) falls back to mode=off', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', maxStackLines: 0 }, + }); + sj.allowErrorProps('stack'); + const e = new Error('test'); + e.stack = 'Error: test\nat app.ts:1:1'; + const { json } = sj.serialize(e); + expect((json as any).stack).toBeUndefined(); + }); + + it('invalid maxStackLines (negative) falls back to mode=off', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', maxStackLines: -1 }, + }); + sj.allowErrorProps('stack'); + const e = new Error('test'); + e.stack = 'Error: test\nat app.ts:1:1'; + const { json } = sj.serialize(e); + expect((json as any).stack).toBeUndefined(); + }); + + it('invalid maxStackLines (non-integer) falls back to mode=off', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', maxStackLines: 1.5 }, + }); + sj.allowErrorProps('stack'); + const e = new Error('test'); + e.stack = 'Error: test\nat app.ts:1:1'; + const { json } = sj.serialize(e); + expect((json as any).stack).toBeUndefined(); + }); + + it('unrecognized stripInternalFrames value falls back to none', () => { + const sj = new SuperJSON({ + errorStack: { + mode: 'string', + stripInternalFrames: 'all_the_things' as any, + }, + }); + sj.allowErrorProps('stack'); + const e = new Error('test'); + e.stack = 'Error: test\nat node:internal/bootstrap.js:1:1'; + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stack).toContain('node:internal'); // not stripped + }); + + it('non-integer maxCauseDepth falls to includeCauses=none', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', includeCauses: 'deep', maxCauseDepth: 1.5 }, + }); + const e = new Error('main', { cause: new Error('cause') }); + const out = sj.parse<any>(sj.stringify(e)); + expect((out as any).cause).toBeUndefined(); + }); + + it('non-integer maxCauseDepth with includeCauses=direct also falls back to none', () => { + const sj = new SuperJSON({ + errorStack: { + mode: 'string', + includeCauses: 'direct', + maxCauseDepth: 2.7, + }, + }); + const e = new Error('outer', { cause: new Error('inner') }); + const out = sj.parse<any>(sj.stringify(e)); + expect((out as any).cause).toBeUndefined(); + }); +}); + +describe('Error Stack – includeCauses option', () => { + it('includeCauses=none discards cause (default)', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const cause = new Error('cause msg'); + const e = new Error('main msg', { cause }); + const out = sj.parse<any>(sj.stringify(e)); + expect(out.cause).toBeUndefined(); + }); + + it('includeCauses=direct includes immediate cause', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', includeCauses: 'direct', maxCauseDepth: 5 }, + }); + sj.allowErrorProps('stack'); + const cause = new Error('root reason'); + const e = new Error('top', { cause }); + const out = sj.parse<any>(sj.stringify(e)); + expect(out.cause).toBeInstanceOf(Error); + expect(out.cause.message).toBe('root reason'); + }); + + it('includeCauses=direct stops at depth 1 regardless of chain', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', includeCauses: 'direct', maxCauseDepth: 5 }, + }); + sj.allowErrorProps('stack'); + const level3 = new Error('level3'); + const level2 = new Error('level2', { cause: level3 }); + const level1 = new Error('level1', { cause: level2 }); + const out = sj.parse<any>(sj.stringify(level1)); + // Only immediate cause preserved; level3 is dropped + expect(out.cause).toBeInstanceOf(Error); + expect(out.cause.message).toBe('level2'); + expect(out.cause.cause).toBeUndefined(); + }); + + it('includeCauses=deep preserves full chain', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', includeCauses: 'deep', maxCauseDepth: 10 }, + }); + sj.allowErrorProps('stack'); + const level3 = new Error('level3'); + const level2 = new Error('level2', { cause: level3 }); + const level1 = new Error('level1', { cause: level2 }); + const out = sj.parse<any>(sj.stringify(level1)); + expect(out.cause.message).toBe('level2'); + expect(out.cause.cause.message).toBe('level3'); + }); + + it('maxCauseDepth=0 discards all causes', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', includeCauses: 'deep', maxCauseDepth: 0 }, + }); + sj.allowErrorProps('stack'); + const cause = new Error('inner'); + const e = new Error('outer', { cause }); + const out = sj.parse<any>(sj.stringify(e)); + expect(out.cause).toBeUndefined(); + }); + + it('non-Error causes are dropped', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', includeCauses: 'direct', maxCauseDepth: 5 }, + }); + sj.allowErrorProps('stack'); + const e = new Error('main'); + (e as any).cause = 'string cause'; // non-Error + const out = sj.parse<any>(sj.stringify(e)); + expect(out.cause).toBeUndefined(); + }); +}); + +describe('Error Stack – maxStackLines', () => { + it('maxStackLines limits included lines (string mode)', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', maxStackLines: 3 }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = [ + 'Error: x', + 'at one.ts:1:1', + 'at two.ts:2:2', + 'at three.ts:3:3', + 'at four.ts:4:4', + ].join('\n'); + const out = sj.parse<any>(sj.stringify(e)); + const lines = out.stack.split('\n'); + expect(lines.length).toBe(3); + }); + + it('maxStackLines counts the header line (line 1)', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', maxStackLines: 1 }, + }); + sj.allowErrorProps('stack'); + const e = new Error('only header'); + e.stack = 'Error: only header\nat app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(e)); + const lines = out.stack!.split('\n'); + expect(lines.length).toBe(1); + expect(lines[0]).toContain('Error: only header'); + }); + + it('maxStackLines limits included lines in frames mode after frame processing', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'frames', maxStackLines: 2, stripInternalFrames: 'node' }, + }); + sj.allowErrorProps('stackFrames'); + const e = new Error('x'); + e.stack = [ + 'Error: x', + 'at node:internal/loaders.js:1:1', + 'at user.ts:2:2', + 'at other.ts:3:3', + ].join('\n'); + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stackFrames).toHaveLength(2); + expect(out.stackFrames[0].raw).toBe('Error: x'); + expect(out.stackFrames[1].raw).toContain('user.ts:2:2'); + }); +}); + +describe('Error Stack – stripInternalFrames', () => { + it('stripInternalFrames=node removes node:internal lines', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', stripInternalFrames: 'node' }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = + 'Error: x\nat /app/app.ts:1:1\nat node:internal/bootstrap.js:2:3'; + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stack).not.toContain('node:internal'); + expect(out.stack).toContain('/app/app.ts'); + }); + + it('stripInternalFrames=node_and_superjson removes node:internal and src/transformer.ts frames', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', stripInternalFrames: 'node_and_superjson' }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = [ + 'Error: x', + 'at /app/entry.ts:1:1', + 'at node:internal/process.js:1:1', + 'at /project/src/transformer.ts:50:10', + ].join('\n'); + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stack).not.toContain('node:internal'); + expect(out.stack).not.toContain('src/transformer.ts'); + expect(out.stack).toContain('entry.ts'); // preserved + }); + + it('header line never stripped even if matching', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', stripInternalFrames: 'node' }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + // header line contains node: but should NOT be stripped + e.stack = 'node:internal/errors.js: internal error\nat app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(e)); + const lines = out.stack!.split('\n'); + expect(lines[0]).toContain('node:internal/errors.js'); + }); +}); + +describe('Error Stack – normalizeNewlines', () => { + it('normalizeNewlines defaults to false when omitted', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = 'Error: x\r\nat app.ts:1:1'; + const outerJson = JSON.parse(sj.stringify(e)); + const stackVal = outerJson.json.stack as string; + expect(stackVal).toContain('\r\n'); + }); + + it('normalizeNewlines=true converts CRLF to LF', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', normalizeNewlines: true }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = 'Error: x\r\nat app.ts:1:1'; + const outerJson = JSON.parse(sj.stringify(e)); + const stackVal = outerJson.json.stack as string; + expect(stackVal).not.toContain('\r\n'); + expect(stackVal).toContain('\n'); + }); + + it('normalizeNewlines=false preserves CRLF', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', normalizeNewlines: false }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = 'Error: x\r\nat app.ts:1:1'; + const outerJson = JSON.parse(sj.stringify(e)); + const stackVal = outerJson.json.stack as string; + expect(stackVal).toContain('\r\n'); + }); + + it('trimLeadingWhitespace defaults to true in string mode', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = ' Error: x\n at app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(e)); + const lines = out.stack.split('\n'); + expect(lines[0]).toBe(' Error: x'); + expect(lines[1]).toBe('at app.ts:1:1'); + }); + + it('trimLeadingWhitespace defaults to true in frames mode', () => { + const sj = new SuperJSON({ errorStack: { mode: 'frames' } }); + sj.allowErrorProps('stackFrames'); + const e = new Error('x'); + e.stack = ' Error: x\n at app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stackFrames[0].raw).toBe(' Error: x'); + expect(out.stackFrames[1].raw).toBe('at app.ts:1:1'); + }); + + it('trimLeadingWhitespace=false preserves leading whitespace in string mode', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', trimLeadingWhitespace: false }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = 'Error: x\n at app.ts:1:1\n at inner.ts:2:3'; + const out = sj.parse<any>(sj.stringify(e)); + const lines = out.stack.split('\n'); + expect(lines[0]).toBe('Error: x'); + expect(lines[1]).toBe(' at app.ts:1:1'); + expect(lines[2]).toBe(' at inner.ts:2:3'); + }); + + it('trimLeadingWhitespace=false preserves leading whitespace in frames mode', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'frames', trimLeadingWhitespace: false }, + }); + sj.allowErrorProps('stackFrames'); + const e = new Error('x'); + e.stack = 'Error: x\n at app.ts:1:1\n at inner.ts:2:3'; + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stackFrames).toHaveLength(3); + expect(out.stackFrames[0].raw).toBe('Error: x'); + expect(out.stackFrames[1].raw).toBe(' at app.ts:1:1'); + expect(out.stackFrames[2].raw).toBe(' at inner.ts:2:3'); + }); + + it('trimLeadingWhitespace=true explicitly trims non-header lines', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', trimLeadingWhitespace: true }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = 'Error: x\n at app.ts:1:1\n at inner.ts:2:3'; + const out = sj.parse<any>(sj.stringify(e)); + const lines = out.stack.split('\n'); + expect(lines[0]).toBe('Error: x'); + expect(lines[1]).toBe('at app.ts:1:1'); + expect(lines[2]).toBe('at inner.ts:2:3'); + }); +}); + +describe('Error Stack – redactPaths', () => { + it('redactPaths=basename replaces full paths with filenames', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', redactPaths: 'basename' }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = 'Error: x\nat /Users/john/projects/app.ts:5:10'; + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stack).not.toContain('/Users/john'); + expect(out.stack).toContain('app.ts'); + }); + + it('redactPaths=strip_cwd removes cwd prefix', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', redactPaths: 'strip_cwd' }, + }); + sj.allowErrorProps('stack'); + const cwd = process.cwd(); + const e = new Error('x'); + e.stack = `Error: x\nat ${cwd}/src/usermodule.ts:5:10`; + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stack).not.toContain(cwd); + expect(out.stack).toContain('src/usermodule.ts'); + }); + + it('redactPaths also applies in frames mode', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'frames', redactPaths: 'basename' }, + }); + sj.allowErrorProps('stackFrames'); + const e = new Error('x'); + e.stack = 'Error: x\nat /Users/john/projects/app.ts:5:10'; + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stackFrames[1].raw).toContain('app.ts'); + expect(out.stackFrames[1].raw).not.toContain('/Users/john'); + }); + + it('string mode applies redactPaths together with maxStackLines', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', redactPaths: 'basename', maxStackLines: 2 }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = [ + 'Error: x', + 'at /Users/john/projects/first.ts:1:1', + 'at /Users/john/projects/second.ts:2:2', + ].join('\n'); + const out = sj.parse<any>(sj.stringify(e)); + const lines = out.stack.split('\n'); + expect(lines).toHaveLength(2); + expect(lines[1]).toContain('first.ts'); + expect(lines[1]).not.toContain('/Users/john'); + expect(out.stack).not.toContain('second.ts'); + }); + + it('string mode applies redactPaths, then maxStackLines, then stripInternalFrames', () => { + const sj = new SuperJSON({ + errorStack: { + mode: 'string', + redactPaths: 'basename', + maxStackLines: 2, + stripInternalFrames: 'node', + }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = [ + 'Error: x', + 'node:internal/process/task_queues:1:1', + 'at /Users/john/projects/keep.ts:2:2', + ].join('\n'); + + const out = sj.parse<any>(sj.stringify(e)); + + expect(out.stack).toBe('Error: x'); + expect(out.stack).not.toContain('keep.ts'); + expect(out.stack).not.toContain('node:internal'); + }); + + it('frames mode applies stripInternalFrames, then redactPaths, then maxStackLines', () => { + const sj = new SuperJSON({ + errorStack: { + mode: 'frames', + redactPaths: 'basename', + maxStackLines: 2, + stripInternalFrames: 'node', + }, + }); + sj.allowErrorProps('stackFrames'); + const e = new Error('x'); + e.stack = [ + 'Error: x', + 'node:internal/process/task_queues:1:1', + 'at /Users/john/projects/keep.ts:2:2', + ].join('\n'); + + const out = sj.parse<any>(sj.stringify(e)); + + // frames mode strips internal frames FIRST (opposite of string mode), + // leaving [header, keep.ts], then basename redaction, then maxStackLines(2) + // → 2 frames, not 1 (which would happen if maxStackLines ran first) + expect(out.stackFrames).toHaveLength(2); + expect(out.stackFrames[0].raw).toBe('Error: x'); + expect(out.stackFrames[1].raw).toContain('keep.ts'); + expect(out.stackFrames[1].raw).not.toContain('/Users/john'); + expect( + out.stackFrames.map((f: any) => f.raw as string).join('\n') + ).not.toContain('node:internal'); + }); + + it('frames mode applies redactPaths together with maxStackLines', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'frames', redactPaths: 'basename', maxStackLines: 2 }, + }); + sj.allowErrorProps('stackFrames'); + const e = new Error('x'); + e.stack = [ + 'Error: x', + 'at /Users/john/projects/first.ts:1:1', + 'at /Users/john/projects/second.ts:2:2', + ].join('\n'); + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stackFrames).toHaveLength(2); + expect(out.stackFrames[1].raw).toContain('first.ts'); + expect(out.stackFrames[1].raw).not.toContain('/Users/john'); + expect(out.stackFrames.map((frame: any) => frame.raw).join('\n')).not.toContain( + 'second.ts' + ); + }); +}); + +describe('Error Stack – AggregateError', () => { + const hasAggregateError = + typeof (globalThis as any).AggregateError !== 'undefined'; + + it.skipIf(!hasAggregateError)( + 'AggregateError serializes .errors array', + () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const agg = new (globalThis as any).AggregateError( + [new Error('e1'), new Error('e2')], + 'multiple errors' + ); + const { json } = sj.serialize(agg); + expect(Array.isArray((json as any).errors)).toBe(true); + } + ); + + it.skipIf(!hasAggregateError)( + 'AggregateError round-trips message', + () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const agg = new (globalThis as any).AggregateError( + [new Error('child')], + 'agg message' + ); + const out = sj.parse<any>(sj.stringify(agg)); + expect(out.message).toBe('agg message'); + } + ); + + it.skipIf(!hasAggregateError)( + 'AggregateError restores .errors on deserialization', + () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + const agg = new (globalThis as any).AggregateError( + [new Error('a'), new Error('b')], + 'agg message' + ); + const out = sj.parse<any>(sj.stringify(agg)); + expect(Array.isArray(out.errors)).toBe(true); + expect(out.errors).toHaveLength(2); + expect(out.errors[0].message).toBe('a'); + expect(out.errors[1].message).toBe('b'); + } + ); +}); + +describe('Error Stack – exported helper functions', () => { + it('normalizeStackNewlines converts CRLF and standalone CR to LF', async () => { + const { normalizeStackNewlines } = await import('./error-stack.js'); + expect(normalizeStackNewlines('a\r\nb\rc\nd')).toBe('a\nb\nc\nd'); + }); + + it('processStackString is exported and applies full pipeline in order', async () => { + const { processStackString } = await import('./error-stack.js'); + const { normalizeErrorStackOptions } = await import('./error-options.js'); + const opts = normalizeErrorStackOptions({ mode: 'string', maxStackLines: 2 })!; + const result = processStackString( + 'Error: x\nat one.ts:1:1\nat two.ts:2:2', + opts + ); + expect(result).toBe('Error: x\nat one.ts:1:1'); + }); + + it('processStackFrames is exported and returns StackFrame array', async () => { + const { processStackFrames } = await import('./error-stack.js'); + const { normalizeErrorStackOptions } = await import('./error-options.js'); + const opts = normalizeErrorStackOptions({ mode: 'frames' })!; + const frames = processStackFrames('Error: x\nat app.ts:1:1', opts); + expect(Array.isArray(frames)).toBe(true); + expect(frames[0]).toHaveProperty('raw', 'Error: x'); + expect(frames[1]).toHaveProperty('raw', 'at app.ts:1:1'); + }); + + it('processStackString with no options returns stack unchanged', async () => { + const { processStackString } = await import('./error-stack.js'); + const { normalizeErrorStackOptions } = await import('./error-options.js'); + const opts = normalizeErrorStackOptions({ mode: 'string' })!; + const stack = 'Error: x\nat app.ts:1:1\nat lib.ts:2:2'; + expect(processStackString(stack, opts)).toBe(stack); + }); + + it('normalizeErrorStackOptions is exported and returns undefined for non-objects', async () => { + const { normalizeErrorStackOptions } = await import('./error-options.js'); + expect(normalizeErrorStackOptions(null)).toBeUndefined(); + expect(normalizeErrorStackOptions('string')).toBeUndefined(); + expect(normalizeErrorStackOptions(undefined)).toBeUndefined(); + }); + + it('normalizeErrorStackOptions fills all normalized fields with correct defaults', async () => { + const { normalizeErrorStackOptions } = await import('./error-options.js'); + const opts = normalizeErrorStackOptions({ mode: 'string' })!; + expect(opts.mode).toBe('string'); + expect(opts.normalizeNewlines).toBe(false); + expect(opts.trimLeadingWhitespace).toBe(true); + expect(opts.stripInternalFrames).toBe('none'); + expect(opts.redactPaths).toBe('none'); + expect(opts.includeCauses).toBe('none'); + expect(typeof opts.maxCauseDepth).toBe('number'); + expect(opts.sanitizeMessage).toBe(false); + }); + + it('sanitizeMessage is exported and replaces all three pattern types', async () => { + const { sanitizeMessage } = await import('./error-sanitizer.js'); + const result = sanitizeMessage( + 'Err at https://api.example.com for user@test.com from 10.0.0.1' + ); + expect(result).toBe('Err at [redacted] for [redacted] from [redacted]'); + }); + + it('ErrorClassRegistry is exported, stores processors by name, and has() works', async () => { + const { ErrorClassRegistry } = await import('./error-class-registry.js'); + const registry = new ErrorClassRegistry(); + const fn = (obj: Record<string, unknown>) => ({ ...obj, tagged: true }); + registry.register('MyError', fn); + expect(registry.has('MyError')).toBe(true); + expect(registry.has('OtherError')).toBe(false); + expect(registry.getProcessor('MyError')).toBe(fn); + expect(registry.getProcessor('OtherError')).toBeUndefined(); + }); +}); + +describe('Error Stack – additional public API behavior', () => { + it('errorStack with missing mode behaves like off', () => { + const sj = new SuperJSON({ errorStack: {} as any }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = 'Error: x\nat app.ts:1:1'; + const { json, meta } = sj.serialize(e); + expect((json as any).stack).toBeUndefined(); + expect(JSON.stringify(meta?.values)).toContain('"Error"'); + expect(JSON.stringify(meta?.values)).not.toContain('Error/stack'); + expect(JSON.stringify(meta?.values)).not.toContain('Error/frames'); + }); + + it('allowErrorProps must opt stack in even when mode=string', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + const e = new Error('x'); + e.stack = 'Error: x\nat app.ts:1:1'; + const { json } = sj.serialize(e); + expect((json as any).stack).toBeUndefined(); + }); + + it('errorStack=undefined behaves like omitting errorStack', () => { + const a = new SuperJSON(); + const b = new SuperJSON({ errorStack: undefined }); + a.allowErrorProps('stack'); + b.allowErrorProps('stack'); + const e = new Error('same'); + expect(JSON.stringify(a.serialize(e))).toBe(JSON.stringify(b.serialize(e))); + }); + + it('errors inside arrays round-trip like standalone errors', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const e = new Error('array error'); + e.stack = 'Error: array error\nat app.ts:1:1'; + const out = sj.parse<any>(sj.stringify([e, 42])); + expect(out[0]).toBeInstanceOf(Error); + expect(out[0].message).toBe('array error'); + expect(typeof out[0].stack).toBe('string'); + }); + + it('errors inside Maps round-trip like standalone errors', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const e = new Error('map error'); + e.stack = 'Error: map error\nat app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(new Map([['key', e]]))); + const outErr = out.get('key'); + expect(out).toBeInstanceOf(Map); + expect(outErr).toBeInstanceOf(Error); + expect(outErr.message).toBe('map error'); + }); + + it('errors inside Sets round-trip like standalone errors', () => { + const sj = new SuperJSON({ errorStack: { mode: 'frames' } }); + sj.allowErrorProps('stackFrames'); + const e = new Error('set error'); + e.stack = 'Error: set error\nat app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(new Set([e]))); + const [outErr] = out; + expect(out).toBeInstanceOf(Set); + expect(outErr).toBeInstanceOf(Error); + expect(Array.isArray(outErr.stackFrames)).toBe(true); + }); + + it('normalizeNewlines=true converts CR-only line endings to LF', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', normalizeNewlines: true }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = 'Error: x\rat app.ts:1:1'; + const outerJson = JSON.parse(sj.stringify(e)); + expect(outerJson.json.stack).toBe('Error: x\nat app.ts:1:1'); + }); + + it('stripInternalFrames=superjson removes only superjson frames', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', stripInternalFrames: 'superjson' }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = [ + 'Error: x', + 'at node:internal/process.js:1:1', + 'at /project/src/plainer.ts:50:10', + ].join('\n'); + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stack).toContain('node:internal'); + expect(out.stack).not.toContain('src/plainer.ts'); + }); + + it('node_and_superjson strips both kinds of frames in frames mode', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'frames', stripInternalFrames: 'node_and_superjson' }, + }); + sj.allowErrorProps('stackFrames'); + const e = new Error('x'); + e.stack = [ + 'Error: x', + 'at /app/handler.ts:1:1', + 'at /lib/src/index.ts:20:3', + 'at node:internal/async_hooks.js:1:1', + ].join('\n'); + const out = sj.parse<any>(sj.stringify(e)); + const raw = out.stackFrames.map((frame: any) => frame.raw).join('\n'); + expect(raw).toContain('handler.ts'); + expect(raw).not.toContain('src/index.ts'); + expect(raw).not.toContain('node:internal'); + }); + + it('unrecognized redactPaths value falls back to none', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', redactPaths: 'unknown_value' as any }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = 'Error: x\nat /project/src/app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stack.split('\n')[1]).toContain('/project/src/app.ts'); + }); + + it('classFilter and sanitizeMessage only affect matched error names', () => { + const sj = new SuperJSON({ + errorStack: { + mode: 'string', + classFilter: ['TypeError'], + sanitizeMessage: true, + }, + }); + const te = new TypeError('fail at https://api.example.com'); + const re = new RangeError('fail at https://api.example.com'); + const { json: jsonTE } = sj.serialize(te); + const { json: jsonRE } = sj.serialize(re); + expect((jsonTE as any).message).toBe('fail at [redacted]'); + expect((jsonRE as any).message).toBe('fail at https://api.example.com'); + }); + + it('non-matching classFilter in frames mode keeps the plain Error annotation', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'frames', classFilter: ['SpecialError'] }, + }); + sj.allowErrorProps('stackFrames'); + const { meta } = sj.serialize(new Error('generic')); + expect(JSON.stringify(meta?.values)).toContain('"Error"'); + expect(JSON.stringify(meta?.values)).not.toContain('Error/frames'); + }); + + it('different SuperJSON instances with different modes do not interfere', () => { + const stringSj = new SuperJSON({ errorStack: { mode: 'string' } }); + const framesSj = new SuperJSON({ errorStack: { mode: 'frames' } }); + stringSj.allowErrorProps('stack'); + framesSj.allowErrorProps('stackFrames'); + const e = new Error('shared'); + e.stack = 'Error: shared\nat app.ts:1:1'; + const { meta: metaString } = stringSj.serialize(e); + const { meta: metaFrames } = framesSj.serialize(e); + expect(JSON.stringify(metaString?.values)).toContain('Error/stack'); + expect(JSON.stringify(metaFrames?.values)).toContain('Error/frames'); + }); + + it('includeCauses=direct with omitted maxCauseDepth still keeps the immediate cause', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', includeCauses: 'direct' }, + }); + sj.allowErrorProps('stack'); + const out = sj.parse<any>( + sj.stringify(new Error('outer', { cause: new Error('inner') })) + ); + expect(out.cause).toBeInstanceOf(Error); + expect(out.cause.message).toBe('inner'); + }); + + it('includeCauses=deep with omitted maxCauseDepth keeps multiple cause levels', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', includeCauses: 'deep' }, + }); + sj.allowErrorProps('stack'); + const level3 = new Error('level3'); + const level2 = new Error('level2', { cause: level3 }); + const level1 = new Error('level1', { cause: level2 }); + const out = sj.parse<any>(sj.stringify(level1)); + expect(out.cause.message).toBe('level2'); + expect(out.cause.cause.message).toBe('level3'); + }); + + it('includeCauses=deep without maxCauseDepth truncates at the default limit of 16', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', includeCauses: 'deep' }, + }); + sj.allowErrorProps('stack'); + + let current: Error = new Error('level17'); + for (let index = 16; index >= 1; index--) { + current = new Error(`level${index}`, { cause: current }); + } + + const root = new Error('level0', { cause: current }); + const out = sj.parse<any>(sj.stringify(root)); + + let cursor = out; + for (let index = 1; index <= 16; index++) { + cursor = cursor.cause; + expect(cursor).toBeInstanceOf(Error); + expect(cursor.message).toBe(`level${index}`); + } + + expect(cursor.cause).toBeUndefined(); + }); + + it('deep cause serialization stops cleanly on circular cause chains', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', includeCauses: 'deep' }, + }); + sj.allowErrorProps('stack'); + const first: any = new Error('first'); + const second: any = new Error('second', { cause: first }); + first.cause = second; + // Must complete without hanging — any finite truncation is valid + const json = sj.stringify(first); + const out = sj.parse<any>(json); + expect(out).toBeInstanceOf(Error); + expect(out.message).toBe('first'); + // Walk whatever chain was produced and confirm it terminates + let cursor: any = out; + let depth = 0; + while (cursor?.cause && depth < 100) { + cursor = cursor.cause; + depth++; + } + // Chain must be finite (not still going at depth 100) + expect(depth).toBeLessThan(100); + }); + + it('sanitizeMessage is NOT applied to cause errors that fail classFilter', () => { + const sj = new SuperJSON({ + errorStack: { + mode: 'string', + sanitizeMessage: true, + classFilter: ['TypeError'], + includeCauses: 'direct', + }, + }); + sj.allowErrorProps('stack'); + const cause = new Error('inner https://api.example.com'); // name='Error', not 'TypeError' + const e = new TypeError('outer https://api.example.com', { cause }); + const { json } = sj.serialize(e); + // TypeError matches classFilter -> sanitized + expect((json as any).message).toBe('outer [redacted]'); + // Error cause does NOT match classFilter -> NOT sanitized + expect((json as any).cause.message).toBe('inner https://api.example.com'); + }); + + it('includeCauses=direct in frames mode: cause round-trips as instanceof Error', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'frames', includeCauses: 'direct' }, + }); + sj.allowErrorProps('stackFrames'); + const cause = new Error('root cause'); + const e = new Error('top', { cause }); + const out = sj.parse<any>(sj.stringify(e)); + expect(out.cause).toBeInstanceOf(Error); + expect(out.cause.message).toBe('root cause'); + }); + + it('AggregateError.errors items are instanceof Error after deserialization', () => { + const hasAggregateError = + typeof (globalThis as any).AggregateError !== 'undefined'; + if (!hasAggregateError) return; + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + const agg = new (globalThis as any).AggregateError( + [new Error('e1'), new TypeError('e2')], + 'multiple' + ); + const out = sj.parse<any>(sj.stringify(agg)); + expect(out.errors[0]).toBeInstanceOf(Error); + expect(out.errors[1]).toBeInstanceOf(Error); + expect(out.errors[0].message).toBe('e1'); + expect(out.errors[1].message).toBe('e2'); + }); + + it('registerErrorStackProcessor fires even when no errorStack option is set', () => { + const sj = new SuperJSON(); // no errorStack + let called = false; + let capturedMsg = ''; + sj.registerErrorStackProcessor('Error', serialized => { + called = true; + capturedMsg = (serialized as any).message ?? ''; + return serialized; + }); + const e = new Error('legacy message'); + sj.serialize(e); + expect(called).toBe(true); + expect(capturedMsg).toBe('legacy message'); + }); + + it('multiple processors for different error names coexist and each fires', () => { + const sj = new SuperJSON({ errorStack: { mode: 'string' } }); + sj.allowErrorProps('stack'); + const calls: string[] = []; + sj.registerErrorStackProcessor('Error', obj => { + calls.push('Error'); + return obj; + }); + sj.registerErrorStackProcessor('TypeError', obj => { + calls.push('TypeError'); + return obj; + }); + sj.serialize(new Error('e')); + sj.serialize(new TypeError('t')); + expect(calls).toEqual(['Error', 'TypeError']); + }); + + it('trimLeadingWhitespace=false combined with redactPaths=basename: whitespace preserved, path redacted', () => { + const sj = new SuperJSON({ + errorStack: { + mode: 'string', + trimLeadingWhitespace: false, + redactPaths: 'basename', + }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = 'Error: x\n at /Users/john/app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(e)); + const lines = out.stack.split('\n'); + // Leading whitespace preserved (trimLeadingWhitespace=false) + expect(lines[1].startsWith(' ')).toBe(true); + // Path redacted to basename + expect(lines[1]).not.toContain('/Users/john'); + expect(lines[1]).toContain('app.ts'); + }); + + it('stripInternalFrames removes all body frames leaving only the header line', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', stripInternalFrames: 'node' }, + }); + sj.allowErrorProps('stack'); + const e = new Error('x'); + e.stack = [ + 'Error: x', + 'at node:internal/bootstrap.js:1:1', + 'at node:internal/process.js:2:2', + ].join('\n'); + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stack).toBe('Error: x'); + }); + + it('normalizeNewlines=true in frames mode normalizes CRLF in each frame raw value', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'frames', normalizeNewlines: true }, + }); + sj.allowErrorProps('stackFrames'); + const e = new Error('x'); + e.stack = 'Error: x\r\nat app.ts:1:1\r\nat lib.ts:2:2'; + const out = sj.parse<any>(sj.stringify(e)); + expect(out.stackFrames).toHaveLength(3); + for (const frame of out.stackFrames) { + expect((frame as any).raw).not.toContain('\r'); + } + }); + + it('registerErrorStackProcessor receives already-redacted paths', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', redactPaths: 'basename' }, + }); + sj.allowErrorProps('stack'); + let capturedStack = ''; + sj.registerErrorStackProcessor('Error', serialized => { + capturedStack = (serialized as any).stack ?? ''; + return serialized; + }); + const e = new Error('x'); + e.stack = 'Error: x\nat /home/user/project/src/app.ts:10:5'; + sj.serialize(e); + // Processor should see the already-redacted stack (basename only, no directory) + expect(capturedStack).not.toContain('/home/user/project/src/'); + expect(capturedStack).toContain('app.ts'); + }); + + it('registerErrorStackProcessor receives already-included cause', () => { + const sj = new SuperJSON({ + errorStack: { mode: 'string', includeCauses: 'direct' }, + }); + sj.allowErrorProps('stack'); + let capturedCause: unknown = 'not-set'; + sj.registerErrorStackProcessor('Error', serialized => { + // Only capture from the outer error (message='outer') + if ((serialized as any).message === 'outer') { + capturedCause = (serialized as any).cause; + } + return serialized; + }); + const cause = new Error('root cause'); + const e = new Error('outer', { cause }); + sj.serialize(e); + // Processor for the outer error should already have cause included + expect(capturedCause).toBeDefined(); + expect(capturedCause).not.toBe('not-set'); + expect((capturedCause as any).message).toBe('root cause'); + }); + + it('mutating the options object after construction has no effect', () => { + const opts: any = { mode: 'string' }; + const sj = new SuperJSON({ errorStack: opts }); + sj.allowErrorProps('stack'); + // Mutate original options after construction + opts.mode = 'off'; + opts.redactPaths = 'basename'; + const e = new Error('test'); + e.stack = 'Error: test\nat /home/user/app.ts:1:1'; + const out = sj.parse<any>(sj.stringify(e)); + // Stack should still be serialized as string (mode=string was normalized at construction) + expect(typeof out.stack).toBe('string'); + // redactPaths=basename mutation should have no effect; original path preserved + expect(out.stack).toContain('/home/user/app.ts'); + }); +}); diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..52c63be --- /dev/null +++ b/test.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")" +MODE="${1:-base}" +case "$MODE" in + base) npx vitest run src/accessDeep.test.ts src/index.test.ts src/is.test.ts src/pathstringifier.test.ts src/registry.test.ts src/transformer.test.ts ;; + new) npx vitest run src/error-stack.test.ts ;; + *) echo "usage: ./test.sh {base|new}"; exit 1 ;; +esac
diff --git a/src/error-stack.test.ts b/src/error-stack.test.ts
new file mode 100644
index 0000000..d0583b5
--- /dev/null
+++ b/src/error-stack.test.ts
@@ -0,0 +1,1522 @@
+import { describe, it, expect } from 'vitest';
+import SuperJSON from './index.js';
+
+describe('Error Stack Serialization – Core', () => {
+ describe('Legacy behavior preserved when no errorStack option', () => {
+ it('serializes error message and name', () => {
+ const sj = new SuperJSON();
+ const e = new Error('hello');
+ const out = sj.parse<Error>(sj.stringify(e));
+ expect(out).toBeInstanceOf(Error);
+ expect(out.message).toBe('hello');
+ });
+
+ it('preserves stack when allowErrorProps is used (legacy)', () => {
+ const sj = new SuperJSON();
+ sj.allowErrorProps('stack');
+ const e = new Error('test');
+ const stack = e.stack!;
+ const out = sj.parse<Error>(sj.stringify(e));
+ expect(out.stack).toBe(stack);
+ });
+
+ it('does not serialize stack when not in allowedErrorProps', () => {
+ const sj = new SuperJSON();
+ const e = new Error('test');
+ const { json } = sj.serialize(e);
+ expect((json as any).stack).toBeUndefined();
+ });
+
+ it('preserves cause in legacy mode', () => {
+ const sj = new SuperJSON();
+ const cause = new Error('root');
+ const e = new Error('top', { cause });
+ const out = sj.parse<Error>(sj.stringify(e));
+ expect((out as any).cause).toBeInstanceOf(Error);
+ expect((out as any).cause.message).toBe('root');
+ });
+
+ it('round-trips custom name', () => {
+ const sj = new SuperJSON();
+ const e = new Error('custom');
+ e.name = 'DBError';
+ const out = sj.parse<Error>(sj.stringify(e));
+ expect(out.name).toBe('DBError');
+ });
+
+ it('uses Error annotation when no errorStack option', () => {
+ const sj = new SuperJSON();
+ const { meta } = sj.serialize(new Error('x'));
+ // annotation must be 'Error', never 'Error/stack' or 'Error/frames'
+ const raw = JSON.stringify(meta?.values);
+ expect(raw).toContain('"Error"');
+ expect(raw).not.toContain('Error/stack');
+ expect(raw).not.toContain('Error/frames');
+ });
+ });
+
+ describe('mode=off behavior', () => {
+ it('mode=off suppresses stack even if allowErrorProps contains stack', () => {
+ const sj = new SuperJSON({ errorStack: { mode: 'off' } });
+ sj.allowErrorProps('stack');
+ const e = new Error('test');
+ e.stack = 'Error: test\nat app.ts:1:1';
+ const { json } = sj.serialize(e);
+ expect((json as any).stack).toBeUndefined();
+ });
+
+ it('mode=off uses "Error" annotation (not Error/off)', () => {
+ const sj = new SuperJSON({ errorStack: { mode: 'off' } });
+ const { meta } = sj.serialize(new Error('x'));
+ const raw = JSON.stringify(meta?.values);
+ expect(raw).toContain('"Error"');
+ expect(raw).not.toContain('Error/off');
+ expect(raw).not.toContain('Error/stack');
+ expect(raw).not.toContain('Error/frames');
+ });
+
+ it('mode=off still preserves name and message', () => {
+ const sj = new SuperJSON({ errorStack: { mode: 'off' } });
+ const e = new TypeError('bad type');
+ const out = sj.parse<Error>(sj.stringify(e));
+ expect(out.name).toBe('TypeError');
+ expect(out.message).toBe('bad type');
+ });
+ });
+
+ describe('mode=string annotations', () => {
+ it('mode=string uses "Error/stack" annotation', () => {
+ const sj = new SuperJSON({ errorStack: { mode: 'string' } });
+ sj.allowErrorProps('stack');
+ const e = new Error('x');
+ e.stack = 'Error: x\nat app.ts:1:1';
+ const { meta } = sj.serialize(e);
+ const raw = JSON.stringify(meta?.values);

Xet Storage Details

Size:
59.6 kB
·
Xet hash:
6e29eea9fca6fd6282453e1abf31565fa34dadb3823a000e03f99009cb1eb6d4

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.