noctcore-contracts/restrict-throw-to-taxonomy
noctcore-contracts/restrict-throw-to-taxonomy
- Package
- contracts v1.1.0
- Preset
- none opt-in
- Severity
- off
- Autofix
- No
- Suggestions
- No
- Options
- Type info
- Not needed
throw only members of your error taxonomy — never an ad hoc built-in nor a bare value.
A codebase that throws a curated set of error types can handle them exhaustively at the boundary:
one place maps AppError → HTTP status, ValidationError → 422, and so on. Throwing an arbitrary
built-in (TypeError, RangeError) or, worse, a bare value (throw 'nope', throw { code })
breaks that: a non-Error carries no stack and no cause, and an unclassified error falls through every
instanceof branch.
What it flags
Section titled “What it flags”throw new SomethingError(...)whose constructor is not in theallowlist.throw <non-Error value>— a string, number, boolean, template literal, object, or array literal.
// built-in not in the taxonomythrow new TypeError('bad');
// bare valuesthrow 'boom';throw { code: 500 };// default allow is ['Error']throw new Error('boom');try { work(); } catch (err) { throw err; }What it does not flag
Section titled “What it does not flag”Conservative on the ambiguous forms. A bare identifier (throw err — the re-throw), a member
(throw ctx.error), and a call (throw makeError()) are all left alone: a syntactic rule cannot know
whether they resolve to an Error, and re-throwing a caught error is the most common throw there is.
Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
allow |
string[] |
['Error'] |
Constructor names permitted as throw targets. Extend it with your base errors and any built-ins you legitimately raise. |
'noctcore-contracts/restrict-throw-to-taxonomy': ['error', { allow: ['AppError', 'ValidationError', 'NotFoundError', 'TypeError'],}]The default ['Error'] is intentionally strict — it assumes a taxonomy rooted at Error and treats
every other built-in as something you should opt into. Widen allow to match your project’s error
model on day one.
The rule is not in recommended: with the default ['Error'], every framework exception
(throw new NotFoundException()) and every built-in other than Error reports, so it only means
something once allow names your classes.
When not to use it
Section titled “When not to use it”If you have no error taxonomy yet, or intentionally throw built-ins throughout, leave this rule off until you have a base error class worth enforcing.