oxc-project/oxc · warning · OxcDiagnostic
Pass a message to the {ctor_name:1} constructor.
Error message
Pass a message to the {ctor_name:1} constructor. What it means
Diagnostic from oxlint's `unicorn/error-message` rule. It requires a message argument when constructing the ten built-in error constructors listed in `BUILT_IN_ERRORS` (Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, InternalError, AggregateError, SuppressedError), in both `new X()` and plain `X()` call form, and only when the identifier resolves to the global reference rather than a local shadow. The message position is argument index 0, except `AggregateError` (1) and `SuppressedError` (2); spread arguments at or before that position are skipped because order is unknowable. This variant means no argument exists at the message position.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/error_message.rs:13
use oxc_ast::{
AstKind,
ast::{Argument, CallExpression, Expression, NewExpression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::IsGlobalReference;
use oxc_span::Span;
use crate::{AstNode, context::LintContext, rule::Rule, utils::BUILT_IN_ERRORS};
fn missing_message(ctor_name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Pass a message to the {ctor_name:1} constructor."))
.with_help(
"A descriptive message makes the error easier to debug when it is caught or logged.",
)
.with_label(span)
}
fn empty_message(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Error message should not be an empty string.")
.with_help("Provide a non-empty string that describes what went wrong.")
.with_label(span)
}
fn not_string(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Error message should be a string.")
.with_help(
"The first argument to an error constructor should be a string describing the error.",
)
.with_label(span)View on GitHub (pinned to e1e7af627c)
Solutions
- Pass a descriptive string: `throw new RangeError('Value must be between 0 and 100')`.
- For `AggregateError`, add the message as the second argument; for `SuppressedError`, as the third.
- If the error is intentionally message-less (rare), disable the rule inline.
Example fix
// before
throw new RangeError();
// after
throw new RangeError('Value must be between 0 and 100'); Defensive patterns
Strategy: validation
Prevention
- Always pass a descriptive string message when constructing built-in errors.
- Remember the message position differs: index 1 for `AggregateError`, index 2 for `SuppressedError`.
- Run oxlint with the unicorn preset in CI so message-less throws surface at review time.
When it happens
Trigger: `throw new Error()`, `throw TypeError()`, or `new AggregateError(errors)` with no message as the second argument — a call/new expression of a global built-in error constructor with zero arguments at the message index.
Common situations: Guard clauses and assertion helpers throwing bare errors; test scaffolding; code migrated from `throw 'str'` patterns; enabling the unicorn preset on an existing codebase.
Related errors
- Error message should not be an empty string.
- Invalid class name, use `{expected}`.
- Missing call to `super()` in constructor.
- The `name` property should be set to `{name}`.
- Error message should be a string.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/54cda16f9cd4df6a.
Report an issue: GitHub.