SuppressedError Preserves Cleanup Fault Chains

Catch SuppressedError thrown when both an operation and DisposableStack cleanup fail, then inspect error and suppressed links across the full chain.

JavascriptResilience

Code

snippet
function runWithFailingCleanup() {
  using stack = new DisposableStack();
  stack.defer(() => {
    throw new Error('close socket failed');
  });
  // Primary body also throws while stack unwinds at block exit
  throw new Error('query timed out');
}

try {
  runWithFailingCleanup();
} catch (err) {
  console.log('caught SuppressedError:', err instanceof SuppressedError);
  let cursor = err;
  let depth = 0;
  // Walk .error (cleanup) and .suppressed (original cause) links
  while (cursor instanceof SuppressedError) {
    console.log(`[${depth}] cleanup=${cursor.error.message} | masked=${cursor.suppressed.message}`);
    cursor = cursor.suppressed;
    depth++;
  }
  console.log('root error:', cursor.message);
}

Output

Captured from a sandboxed run of this snippet in the build pipeline.

caught SuppressedError: true
[0] cleanup=close socket failed | masked=query timed out
root error: query timed out

Annotated execution — SuppressedError Preserves Cleanup Fault Chains

SuppressedError Preserves Cleanup Fault Chains comes down to the function runWithFailingCleanup.

The file repeats itself only inside while (cursor instanceof SuppressedError). A Error is thrown unconditionally at its throw site — no input check sits in front of it.

Points of care

  • It is dependency-free in the strict sense — the import list, if any, is empty of packages.

Putting it to use

Featured callable header defines function runWithFailingCleanup()

Output lines are prefixed (caught SuppressedError:, root error:), making sequential computation results transparent on every line printed.

Core lines

throw new Error('close socket failed');
throw new Error('query timed out');

Interactive Tools for This Topic

FAQ

Does the snippet validate its input?
No. There is no input validation in the snippet — the `Error` is raised unconditionally at its site, with no check in front of it.
Can I paste this into a project as-is?
Yes. Paste it in and run; no package manager step is needed. The file defines `runWithFailingCleanup` at load time.