{"record":{"id":"8c70b78ed594f795","repo":"oxc-project/oxc","slug":"prefer-catch-to-then-a-b-or-then-null-b","errorCode":null,"errorMessage":"Prefer `catch` to `then(a, b)` or `then(null, b)`","messagePattern":"Prefer `catch` to `then\\(a, b\\)` or `then\\(null, b\\)`","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/promise/prefer_catch.rs","lineNumber":9,"sourceCode":"use oxc_ast::{AstKind, ast::Expression};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn prefer_catch_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer `catch` to `then(a, b)` or `then(null, b)`\")\n        .with_help(\n            \"Handle promise errors in a `catch` instead of using the second argument of `then`.\",\n        )\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferCatch;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prefer `catch` to `then(a, b)` and `then(null, b)`. This rule disallows the passing of an\n    /// argument into the second parameter of `then` calls for handling promise errors.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// A `then` call with two arguments can make it more difficult to recognize that a catch error","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/promise/prefer_catch.rs#L1-L27","documentation":"Diagnostic from the oxlint rule `promise/prefer-catch` (plugin `promise`). It flags the two-argument error-handler form of `then`: `then(a, b)` and `then(null, b)`. The second argument only handles rejections from upstream - it does NOT catch errors thrown inside the success handler `a` - whereas a chained `.catch()` handles both. This asymmetry is the reason the pattern is considered an anti-pattern.","triggerScenarios":"`p.then(onSuccess, onFailure)`; `p.then(null, onFailure)` used as a catch substitute.","commonSituations":"Code ported from other promise libraries where the pair form was idiomatic; authors avoiding a second chain link for brevity; older tutorials.","solutions":["Split the handlers: `p.then(onSuccess).catch(onFailure)`","If `onFailure` really must only cover upstream rejections, document it and suppress the rule inline"],"exampleFix":"// before\np.then(render, showError)\n\n// after\np.then(render).catch(showError)","handlingStrategy":"validation","validationCode":"npx oxlint --promise/prefer-catch src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never write `then(a, b)`; use `.then(a).catch(b)` so handler errors are also caught","Remember the second `then` argument misses errors thrown inside the first handler","Grep for `, onFailure)` patterns and `then(null,` during lint-rollout cleanups"],"tags":["promise","async","lint","error-handling","oxlint"],"backgroundTag":"then-second-argument-error-handler","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"}