{"record":{"id":"a4d13ca2f43f967c","repo":"oxc-project/oxc","slug":"error-message-should-be-a-string","errorCode":null,"errorMessage":"Error message should be a string.","messagePattern":"Error message should be a string\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/error_message.rs","lineNumber":27,"sourceCode":"\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)\n}\n\n#[derive(Default, Debug, Clone)]\npub struct ErrorMessage;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Enforces providing a `message` when creating built-in `Error` objects to\n    /// improve readability and debugging.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Throwing an `Error` without a message, like `throw new Error()`, provides no context","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/error_message.rs#L9-L45","documentation":"Diagnostic from oxlint's `unicorn/error-message` rule. The message argument is an array literal `[...]` or object literal `{...}` — non-string literals that are almost certainly a mistake, since `Error` stringifies them into useless text like the joined array items. The rule checks only these literal forms at the message position of the built-in error constructors; identifiers, calls, and other expressions are not flagged.","triggerScenarios":"`throw new Error(['code missing', 'host missing'])` or `throw new ValidationError({ field: 'email' })` where the callee is a global built-in error constructor (for user-defined error classes the rule does not fire).","commonSituations":"Validation code collecting multiple issues and passing the array directly; refactors where an options object was mistakenly put in the message slot.","solutions":["Join arrays into a single string: `new Error(issues.join(', '))`.","Serialize objects deliberately: `new Error(JSON.stringify(details))`, or keep details on a custom property and pass a summary string as the message.","Subclass Error if you need structured payloads instead of forcing them into the message."],"exampleFix":"// before\nthrow new Error(['code missing', 'host missing']);\n// after\nthrow new Error('Validation failed: ' + ['code missing', 'host missing'].join(', '));","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass a single string as the error message; join arrays and stringify objects before constructing the error.","Carry structured payloads on custom error subclasses instead of the message slot.","Treat `new Error([...])`/`new Error({...})` as review-blocking smells."],"tags":["oxlint","lint","unicorn","errors","types"],"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-14T00:17:10.932Z"}