{"record":{"id":"54cda16f9cd4df6a","repo":"oxc-project/oxc","slug":"pass-a-message-to-the-ctor-name-1-constructor","errorCode":null,"errorMessage":"Pass a message to the {ctor_name:1} constructor.","messagePattern":"Pass a message to the (.+?) constructor\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/error_message.rs","lineNumber":13,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Argument, CallExpression, Expression, NewExpression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::IsGlobalReference;\nuse oxc_span::Span;\n\nuse crate::{AstNode, context::LintContext, rule::Rule, utils::BUILT_IN_ERRORS};\n\nfn missing_message(ctor_name: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Pass a message to the {ctor_name:1} constructor.\"))\n        .with_help(\n            \"A descriptive message makes the error easier to debug when it is caught or logged.\",\n        )\n        .with_label(span)\n}\n\nfn empty_message(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Error message should not be an empty string.\")\n        .with_help(\"Provide a non-empty string that describes what went wrong.\")\n        .with_label(span)\n}\n\nfn not_string(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Error message should be a string.\")\n        .with_help(\n            \"The first argument to an error constructor should be a string describing the error.\",\n        )\n        .with_label(span)","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/error_message.rs#L1-L31","documentation":"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.","triggerScenarios":"`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.","commonSituations":"Guard clauses and assertion helpers throwing bare errors; test scaffolding; code migrated from `throw 'str'` patterns; enabling the unicorn preset on an existing codebase.","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."],"exampleFix":"// before\nthrow new RangeError();\n// after\nthrow new RangeError('Value must be between 0 and 100');","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["oxlint","lint","unicorn","errors","debugging"],"backgroundTag":"error-constructor-message","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}