proposal-explicit-resource-management: Suppressed exceptions during dispose
Imagine that as you catch the main exception, you get more exceptions during implicit calls to dispose methods (ex. remote filestystem has been lost):
async function copy(first, second) {
using(let firstFile = await open(first)) {
using(let otherFile = await open(second)) {
if( someCondition ) {
throw new Error( 'Some error' );
}
}
}
}
try {
await copy( 'first.txt', 'second.txt' );
} catch (e) {
console.log( e.message ); // 'Some error'
console.log( e.suppressed ); // [ Error('Device unavailable'), Error('Broken pipe') ]
}
In Java, implicit exceptions are suppressed and added to the main. Is a similar solution planned here?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (6 by maintainers)
I went with
AggregateErrorwhereerrorsare the errors from dispose andcauseis the body error (if present). If there are no errors from dispose, the body error is thrown directly.Fixed in 7968c49e482dcf40a9a942b9ad8aaf26daf0240e
My source of concern is current expectations around how
AggregateErrorgenerally works. If this were a separate error I would probably be less concerned. I think I am fine with the body error beingcausehowever, since this is new syntax and MDN or other sites could document what to expect.… though if we did go the meta-property route, it would be nice to have other meta-properties that could be used in
finallyas well:throw.hasError—truein acatchclause, or in afinallyblock if propagating an uncaught error;falseany other time..errored,.hasFault,.faulted,.hasException,.excepted,.wasThrown, etc.throw.error— The thrown error that caused us to jump tocatchorfinally(if there was one)..fault,.exception,.thrown,.cause,.reason, etc.throw.suppressed— AnAggregateErrorfor any errors thrown during disposal.I’ve had more than a few cases where I’ve written something like this:
It would also be nice to have just a plain
throw;statement in acatchclause that rethrows the caught exception (C# has this, not sure about other languages offhand). Might be worth a separate proposal, and I’ve been thinking about it since we added bindinglesscatch:That seems confusing; why not always make it an aggregate error?
Promise.allSettleddoesn’t skip the AggregateError when there’s only one rejection; that forces consumers to duck-type and have two code paths.