Repository navigation
fix: stop flattenError at a cyclic error cause - #393
ofri-peretz wants to merge 2 commits into
Conversation
rexxars
left a comment
There was a problem hiding this comment.
Could we keep seen local to each branch? With the shared set, two errors that share a cause get treated as a cycle, even when there isn't one:
const cause = new Error('ECONNREFUSED')
const error = new AggregateError([
new Error('IPv6', {cause}),
new Error('IPv4', {cause}),
])This previously produced Error: IPv6: ECONNREFUSED, Error: IPv4: ECONNREFUSED, but now produces Error: IPv6: ECONNREFUSED, IPv4. The cause is still in the overall message, but we lose its connection to the second error.
Replacing seen.add(err) with this would preserve that information while still stopping cycles:
seen = new Set(seen).add(err)We can also drop the 'cause' in err check. Reading a missing cause gives undefined, which the instanceof check already handles:
if (err.cause instanceof Error && !seen.has(err.cause)) {
return `${err}: ${flattenError(err.cause, seen)}`
}Those two changes together came out slightly smaller than the current patch when minifying and compressing the full bundle with Terser and gzip/Brotli, though we're talking about a few bytes.
Would be good to cover the shared-cause case, a self-cause, and a cycle through AggregateError.errors too. The current test closes after the first error event, so it doesn't verify the reconnect mentioned in the changeset. Could we have it wait for another fetch attempt as well?
Siblings in an AggregateError now get their own copy of the set, so two errors with a common cause both print it. Drops the redundant 'cause' in err check, and tests a self-cause, an AggregateError cycle, a shared cause, and the reconnect after a cyclic-cause error.
|
Thanks @rexxars, all four done in a76fe1a:
|
flattenErrorfollowserr.cause(anderr.errors) with no stop. Iffetch()rejects with an error whose cause chain loops back on itself, it throwsRangeError: Maximum call stack size exceededinside the failure handler. The rejection goes unhandled, noerrorevent fires, and the reconnect is never scheduled.The fix passes a set of errors already flattened through the recursion and stops at one it has seen. It also stops passing
map's index as a second argument. There's a new test with a fetch that rejects with a cyclic cause. It fails with theRangeErrorbefore the change, and the whole suite passes after.I found this with
no-unbounded-cause-recursionfrom eslint-plugin-reliability, a plugin I maintain. The patch was prepared with an AI assistant (Claude Code). I reviewed it and ran the tests.