Narrow try/catch by error kind: a catch handles only what it doesn't rethrow #18
Loading…
Reference in a new issue
No description provided.
Delete branch "catch-narrowing"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Stacked on #11 (
promise-returning-functions, itself on #5): it builds on #11's per-kind thrown/rejected tracking inproject.rs. Review #5 and #11 first; this PR's own change is the last commit.Closes the third item of README "Known gaps": any
catchcounted as handling every error.if (e instanceof NotFound) return x; throw e;hidForbiddenfrom the analysis,catch (e) { throw e }looked like a handler, andtry { ... } finally { ... }without acatchcounted as one too.What changed
analyze.rs,catch_rethrows): for eachcatch (e), when does it rethrowe? Followed through blocks,if/else,return,throw e(andthrowof something else, which handlese), with conditions built frome instanceof X(Xorns.X),!,&&,||. Any other test, and loops /switch/ labels / nestedtrythat contain athrow e, count as maybe rethrowing (three-valued logic). A catch that never rethrows handles everything (as before); one that always does, andfinallywithoutcatch, handle nothing; the rest are recorded per site (tries).project.rs):e instanceof Xis evaluated for eachPropagateErrorkind along the thrown class'sextendschain, across files and re-exports (Gone extends NotFoundis caught byinstanceof NotFound, not the other way round;instanceof PropagateError/Errorcatch all kinds). A kind is caught only if the catch's rethrow condition is definitely false for it; otherwise it keeps propagating.safe-ts check's map lists, per call, only the kinds caught there; the others are reported or propagate as usual. Works with #11's rules: a rejection is only caught by atryit is awaited in.tryare decided in the whole-program pass: handled only if the catch keeps them whatever they are, or keepsinstanceof Error; a test against a projectPropagateErrorclass is known not to match them. Socatch (e) { if (e instanceof NotFound) return; throw e; }aroundJSON.parsenow reportsunhandled-failure.Rule change to review:
no-throwandthrow ethrow eof the unchangedcatch (e)binding is no longer ano-throw. Without that, theelse throw epattern this item asks to understand can't be written in checked code at all. It stays sound: thetryis now known not to handle what is rethrown, so it is reported where it starts (theno-throwpage's rethrow example now shows theunhandled-failureonJSON.parseinstead). Reassigned bindings (e = ...) andthrowof anything else are stillno-throw. If you'd rather keepno-throwstrict, this hunk can be dropped and the narrowing still applies to legacy code and to the map.Not covered (documented in README "Known gaps" and
uncaught-propagation)Tests other than
instanceof(e.name === ...,isRetryable(e)), aliases ofe, and loops/switchin acatcharen't read in detail: if they can reachthrow e, every kind counts as rethrown. For built-in and library failures the error class isn't tracked, soif (e instanceof SyntaxError) return; throw e;aroundJSON.parseis still reported (a false positive, in the safe direction). There is nodocs/overview.mdin the repo.Tests
tests/fixtures/catches:else throw, early return thenthrow e, negated test withns.X,||, subclass-only test,instanceof PropagateError, a handled branch that falls through to the rethrow, an unknown test, a catch without rethrow,finallyonly, nested tries, async withawait, andJSON.parse/ legacy calls under narrowing catches (reported when rethrown, not when kept). Fixturethrows:catch (e) { throw e }now propagates both kinds and is not ano-throw(it previously expectedcaught, encoding the gap). Both fail against #11's code (checked by restoringsrcfrom that branch).good) anduncaught-propagation(bad); theno-throwrethrow example changed as above.cargo nextest run --workspace: 94 passed, 1 failed: the pre-existingfeaturesfixture failure onmain(createDecipheriv,main.ts:55), unrelated.npm run check(demo): output identical tomain.safe-ts check examples/vue/src/main.ts: no problems.npm run test:vite,npm run test:docs: passed.npm run test:std: 115/116;feature-nodemanifest test depends on the local Node version (v26.13.0), fails the same onmain.🤖 Generated with Claude Code
Any `catch` counted as handling every error, so `if (e instanceof NotFound) return x; throw e;` hid `Forbidden` from the analysis, `catch (e) { throw e }` looked like a handler, and even `try { ... } finally { ... }` without a catch counted as one. Each catch clause is read for when it rethrows its binding: `if`/`else`, `return`, blocks and `throw e`, with conditions built from `e instanceof X` (`X` or `ns.X`), `!`, `&&` and `||`. Anything else it can't read (other tests, loops, switch) counts as maybe rethrowing. The whole-program pass decides `instanceof` per kind along the thrown class's `extends` chain, across files, so only the kinds a catch really keeps are removed; the rest keep propagating and the PropagateError map lists, per call, only the kinds caught there. Built-in, library and legacy failures count as handled only if the catch keeps them whatever they are (or keeps `instanceof Error`). `throw e` of the unchanged catch binding is no longer a `no-throw`: without it the narrowing pattern can't be written in checked code, and what it rethrows is still reported where it starts, since the try is now known not to handle it. Co-Authored-By: Claude Opus 5.5 <[email protected]>