Sub topic 8
When Cleanup Fails: SuppressedError
This chapter assumes you know how try/catch/finally propagates errors, and how using disposes resources in reverse order when a block exits. Those were covered in earlier chapters.
Two errors, one throw
A finally block can throw an error of its own. If the try block had already thrown, the error from finally replaces the original, and the original is lost. With manual cleanup, that is how the root cause of a failure goes missing.
using handles the same case without losing anything. If the block throws and a disposer also throws, the runtime keeps both errors. It wraps them in a SuppressedError, which has two properties:
| Property | Holds |
|---|---|
| `error` | The newer error, the one thrown by the disposer |
| `suppressed` | The earlier error that was pending when the disposer threw |
try {
using res = {
[Symbol.dispose]() {
throw new Error("cleanup failed");
},
};
throw new Error("body failed");
} catch (e) {
console.log(e instanceof SuppressedError); // true
console.log(e.error.message); // "cleanup failed"
console.log(e.suppressed.message); // "body failed"
}People often read these two properties backwards. error is the latest failure, and suppressed is what it pushed aside.
When no wrapping happens
A SuppressedError is created only when an error is already pending and another one is thrown during disposal:
| Body | Disposer | What propagates |
|---|---|---|
| Succeeds | Succeeds | Nothing |
| Throws | Succeeds | The body's error, unchanged |
| Succeeds | Throws | The disposer's error, unchanged |
| Throws | Throws | `SuppressedError` |
Code that checks e instanceof SuppressedError therefore only matches the last row. In the other rows you get a plain error.
Chains from multiple resources
Every remaining resource is still disposed after one disposer throws. Each new failure wraps the current pending error, so several failures build a nested chain:
function failing(name) {
return {
[Symbol.dispose]() {
throw new Error(`dispose ${name} failed`);
},
};
}
function describe(e, depth = 0) {
const pad = " ".repeat(depth);
if (e instanceof SuppressedError) {
console.log(`${pad}SuppressedError`);
console.log(`${pad} error: ${e.error.message}`);
describe(e.suppressed, depth + 1);
} else {
console.log(`${pad}original: ${e.message}`);
}
}
try {
using a = failing("a");
using b = failing("b");
throw new Error("body failed");
} catch (e) {
describe(e);
}
// SuppressedError
// error: dispose a failed
// SuppressedError
// error: dispose b failed
// original: body failedBecause b is disposed first, its error wraps the body's error. Then a is disposed, and its error wraps that SuppressedError. The outermost error belongs to the last disposer that ran. The original failure sits at the bottom of the chain.
Creating one yourself
SuppressedError is a global constructor with the signature new SuppressedError(error, suppressed, message). You can use it in your own cleanup helpers so their errors match what using produces:
const err = new SuppressedError(
new Error("flush failed"),
new Error("write failed"),
"Write and flush both failed"
);
console.log(err.message); // "Write and flush both failed"
console.log(err.error.message); // "flush failed"
console.log(err.suppressed.message); // "write failed"await using follows the same rules. A rejected Symbol.asyncDispose promise counts as a thrown disposer error.
Exercise
A block declares using x and then using y. The body completes normally. Both disposers throw: y throws "Y" and x throws "X". What is thrown, and what do its properties contain?
Show answer
A SuppressedError is thrown. y is disposed first and its error "Y" becomes the pending error, unwrapped, because nothing was pending before it. Then x throws "X" while "Y" is pending, so the result is a SuppressedError whose error is "X" and whose suppressed is "Y".
Exercise
The body throws "body", and the single resource's disposer completes without error. Is e instanceof SuppressedError true in the catch block?
Show answer
No. No second error occurred, so the original "body" error propagates unchanged.