@@ -17,12 +17,15 @@ import { describe, it, expect, vi } from 'vitest';
1717import { logServerFault , faultCauseChain , FAULT_CAUSE_DEPTH_LIMIT , SERVER_FAULT_LOG_PREFIX } from './server-fault-log.js' ;
1818import { sendServerFault } from './response-envelope.js' ;
1919
20+ /** `new Error(message, { cause })` without the ES2022 lib this package does not compile against. */
21+ const withCause = ( message : string , cause : unknown ) : Error => Object . assign ( new Error ( message ) , { cause } ) ;
22+
2023const ROOT = 'SQLITE_IOERR: disk I/O error reading policy_revision.version' ;
2124const MID = 'driver read failed for policy_revision' ;
2225const OUTER = 'The related records of policy could not be read.' ;
2326
2427/** wrapper → mid → root, the shape a door wraps a driver error in. */
25- const wrapped = ( ) => new Error ( OUTER , { cause : new Error ( MID , { cause : new Error ( ROOT ) } ) } ) ;
28+ const wrapped = ( ) => withCause ( OUTER , withCause ( MID , new Error ( ROOT ) ) ) ;
2629
2730const faultLines = ( spy : { mock : { calls : unknown [ ] [ ] } } ) =>
2831 spy . mock . calls . filter ( ( c ) => String ( c [ 0 ] ) . startsWith ( SERVER_FAULT_LOG_PREFIX ) ) ;
@@ -77,7 +80,7 @@ describe('logServerFault — the console fallback prints the cause chain', () =>
7780describe ( 'faultCauseChain — bounded both ways' , ( ) => {
7881 it ( 'stops a cycle at its first repeat, and says so' , ( ) => {
7982 const a = new Error ( 'a' ) ;
80- const b = new Error ( 'b' , { cause : a } ) ;
83+ const b = withCause ( 'b' , a ) ;
8184 ( a as { cause ?: unknown } ) . cause = b ;
8285 const chain = faultCauseChain ( a ) ;
8386 expect ( chain . map ( ( l ) => l . message ) ) . toEqual ( [
@@ -94,7 +97,7 @@ describe('faultCauseChain — bounded both ways', () => {
9497
9598 it ( `stops a deep chain after ${ FAULT_CAUSE_DEPTH_LIMIT } links, and says so` , ( ) => {
9699 let error = new Error ( 'link 0' ) ;
97- for ( let i = 1 ; i <= 20 ; i += 1 ) error = new Error ( `link ${ i } ` , { cause : error } ) ;
100+ for ( let i = 1 ; i <= 20 ; i += 1 ) error = withCause ( `link ${ i } ` , error ) ;
98101 const chain = faultCauseChain ( error ) ;
99102 expect ( chain ) . toHaveLength ( FAULT_CAUSE_DEPTH_LIMIT + 1 ) ;
100103 expect ( chain . slice ( 0 , FAULT_CAUSE_DEPTH_LIMIT ) . map ( ( l ) => l . message ) ) . toEqual (
@@ -104,8 +107,8 @@ describe('faultCauseChain — bounded both ways', () => {
104107 } ) ;
105108
106109 it ( 'prints a non-Error cause as its text, and ends the chain there' , ( ) => {
107- expect ( faultCauseChain ( new Error ( 'w' , { cause : 'socket hang up' } ) ) ) . toEqual ( [ { message : 'socket hang up' } ] ) ;
108- expect ( faultCauseChain ( new Error ( 'w' , { cause : { code : 'ECONNRESET' } } ) ) ) . toEqual ( [ { message : '{"code":"ECONNRESET"}' } ] ) ;
110+ expect ( faultCauseChain ( withCause ( 'w' , 'socket hang up' ) ) ) . toEqual ( [ { message : 'socket hang up' } ] ) ;
111+ expect ( faultCauseChain ( withCause ( 'w' , { code : 'ECONNRESET' } ) ) ) . toEqual ( [ { message : '{"code":"ECONNRESET"}' } ] ) ;
109112 } ) ;
110113
111114 it ( 'a throwing cause getter ends the chain rather than costing the line' , ( ) => {
@@ -125,7 +128,7 @@ describe('logServerFault — an injected logger renders the chain itself', () =>
125128 expect ( logger . error ) . toHaveBeenCalledTimes ( 1 ) ;
126129 const [ , err , meta ] = logger . error . mock . calls [ 0 ] ;
127130 expect ( err ) . toBe ( thrown ) ;
128- expect ( ( err as Error ) . cause ) . toBeInstanceOf ( Error ) ;
131+ expect ( ( err as { cause ?: unknown } ) . cause ) . toBeInstanceOf ( Error ) ;
129132 expect ( meta ) . not . toHaveProperty ( 'causes' ) ;
130133 } ) ;
131134} ) ;
0 commit comments