Skip to content
Esc

noctcore-contracts/no-error-stringify

Stringifying an error with ${error}, error.toString(), or error + "" gives “Error: ” or “[object Object]”.

`${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:

Correct
error instanceof Error ? error.message : String(error)

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):

Incorrect · 5 reports
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.

Correct
// the guarded idiom (bare String(error) is intentionally NOT policed)
const msg = error instanceof Error ? error.message : String(error);
const m = `${error.message}`;
  • Bare String(error), which the guarded extractor idiom relies on.
  • Member reads such as `${error.message}` or error.stack.
  • x + "" where x is not one of errorIdentifierNames (count + "").
  • Anything inside a logger call’s arguments (logger.error(\failed: ${err}`)), unless checkLoggerArguments is 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's error.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.
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'] }]

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.