noctcore-contracts/no-error-stringify
noctcore-contracts/no-error-stringify
- Package
- contracts v1.1.0
- Preset
- recommended
- Severity
- error
- Autofix
- No
- Suggestions
- Yes
- Options
- Optional
- Type info
- Not needed
Stringifying an error with ${error}, error.toString(), or error + "" gives “Error:
`${error}`, error.toString(), and error + "" coerce an Error to "Error: <message>" (the
class name glued to the message) and a thrown plain object to "[object Object]", discarding
error.cause, the stack, and any custom fields. The guarded extractor idiom gives a readable message
and stays legal; to keep the stack and cause, pass the error object itself to a structured logger:
error instanceof Error ? error.message : String(error)What it flags
Section titled “What it flags”Only the three unambiguous forms, and only when the operand holds an error: any name bound by a
catch (x) clause or by a rejection handler (.catch((x) => ...), the second callback of .then),
plus any identifier named in errorIdentifierNames (default error, err):
const text = `request failed: ${error}`;const a = err.toString();const b = error + "";try { run(); } catch (e) { log("" + e); }load().catch((cause) => log(`failed: ${cause}`));Each report offers a suggestion that rewrites the expression to the guarded idiom.
// the guarded idiom (bare String(error) is intentionally NOT policed)const msg = error instanceof Error ? error.message : String(error);const m = `${error.message}`;What it does not flag
Section titled “What it does not flag”- Bare
String(error), which the guarded extractor idiom relies on. - Member reads such as
`${error.message}`orerror.stack. x + ""wherexis not one oferrorIdentifierNames(count + "").- Anything inside a logger call’s arguments (
logger.error(\failed: ${err}`)), unlesscheckLoggerArgumentsis on. Observability [no-error-detail-loss](https://noctcore.github.io/eslint-plugins/rules/observability/no-error-detail-loss/) owns that line and asks for the error object itself, which keeps the stack and cause; this rule'serror.messageadvice would drop them.loggersandmethods` take the same values as there. - Short names that a catch did not bind:
sizes.map((e) => \${e}px`), ``because ${cause}` `` with a string parameter.
Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
errorIdentifierNames |
string[] |
['error', 'err'] |
Identifier names treated as errors wherever they appear. Catch-bound names count without being listed. |
checkLoggerArguments |
boolean |
false |
Also report inside a logger call’s arguments. |
loggers |
string[] |
['console', 'logger', 'log'] |
Logger object names whose calls are skipped (a bare name or the last segment of this.logger). |
methods |
string[] |
['info', 'warn', 'error', 'debug', 'log', 'trace', 'fatal', 'verbose'] |
Log-level methods whose calls are skipped. |
'noctcore-contracts/no-error-stringify': ['error', { errorIdentifierNames: ['error', 'cause', 'failure'] }]When not to use it
Section titled “When not to use it”If your codebase deliberately renders errors to strings at the boundary (and has already extracted the
cause), or names error bindings unpredictably, this rule may be noisy — narrow or widen
errorIdentifierNames to fit.