{"record":{"id":"63f0c3cf12dd1681","repo":"oxc-project/oxc","slug":"do-not-throw-undefined","errorCode":null,"errorMessage":"Do not throw undefined","messagePattern":"Do not throw undefined","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/eslint/no_throw_literal.rs","lineNumber":12,"sourceCode":"use oxc_ast::{AstKind, ast::Expression};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, ast_util::could_be_error, context::LintContext, rule::Rule};\n\nfn no_throw_literal_diagnostic(span: Span, is_undef: bool) -> OxcDiagnostic {\n    let message =\n        if is_undef { \"Do not throw undefined\" } else { \"Expected an error object to be thrown\" };\n\n    OxcDiagnostic::warn(message)\n        .with_help(\"Throwing literals or non-Error objects is not recommended. Use an Error object instead.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoThrowLiteral;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallows throwing literals or non-Error objects as exceptions.\n    ///\n    /// ::: warning\n    /// This rule has been deprecated, please instead use [typescript/only-throw-error](https://oxc.rs/docs/guide/usage/linter/rules/typescript/only-throw-error.html).\n    /// The typescript rule is more reliable than the Javascript version, as it has less false positive, and can catch more cases.\n    /// :::\n    ///\n    /// ### Why is this bad?","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_throw_literal.rs#L1-L30","documentation":"The `is_undef` variant of `no-throw-literal`: it fires specifically on `throw undefined;`. Throwing `undefined` is the worst case of literal throwing — the catch site receives nothing at all, `e instanceof Error` is false, and loggers print `undefined` with no message or stack. The rule distinguishes it with the dedicated message \"Do not throw undefined\" while sharing the same help text.","triggerScenarios":"`throw undefined;` where the thrown expression resolves to the `undefined` identifier or `void 0`. Common after refactors where a variable holding an error is renamed/deleted and the throw left behind.","commonSituations":"Refactoring `throw err` into code where `err` became undefined; placeholder throws (`throw TODO`) written during stubbing; transpiled code from older reject(undefined) promise patterns.","solutions":["Replace with a real error: `throw new Error('...')` describing why the code path is unreachable or invalid.","If this was a stub, implement the real error path instead of throwing undefined.","Audit sibling throws in the same file for the same refactor residue."],"exampleFix":"// before\ntry {\n  parse(input);\n} catch (err) {\n  throw undefined; // loses everything\n}\n\n// after\ntry {\n  parse(input);\n} catch (err) {\n  throw new Error('parse failed', { cause: err });\n}","handlingStrategy":"type-guard","validationCode":"// never let a refactor leave a bare undefined throw\nfunction required(value: unknown, msg = 'unreachable'): Error {\n  return new Error(msg);\n}\n// use: throw required(x);","typeGuard":"function isThrownable(value: unknown): value is Error {\n  return value instanceof Error;\n}","tryCatchPattern":"catch (e) {\n  if (!(e instanceof Error)) throw new Error('unnamed failure'); // undefined throw lands here\n  throw e;\n}","preventionTips":["Treat `throw undefined` as a stub marker and never merge it.","When renaming/deleting error variables, re-run the linter on the file immediately.","Keep no-throw-literal enabled so the stub is caught in CI."],"tags":["eslint","oxlint","no-throw-literal","undefined","throw","exceptions"],"backgroundTag":"throwing-non-error","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"}