把错误吞掉,让它“能跑”
Errors swallowed to make it "work"
智能体用一个什么都不做(或只打日志)的 catch 包住出错的代码,错误消失了,“出问题了”的信号也跟着消失了。
适用于: Claude Code · Cursor · Codex · Copilot agent
症状
出现 `catch (e) {}`、`except: pass`、用 `except Exception:` 替换原本精确的异常处理,或者 catch 里返回 `null`,让坏数据流向下游。
为什么会这样
崩溃是看得见的失败;吞掉错误则让任务看起来完成、测试看起来通过。agent-review 列了 AR017(静默/吞掉的 catch)和 AR019(过宽的异常捕获),并指出当真正的问题在上游时,智能体会放宽 catch 来让测试通过。
示例
摘自 agent-review 分类(AR017)。
jstry { fn() } catch (e) { /* swallow */ }
复制即用的修复
按你用的智能体挑选文件。规则片段用英文写,放进任何仓库都能用。
规则
Claude Code:加入 CLAUDE.md
CLAUDE.md## Don't hide errors
- Never add an empty catch, `except: pass`, or a catch that only logs and continues, unless the user asked
for it and you comment why the error is safe to ignore.
- Catch the narrowest error type you can handle, and handle it (retry, fallback with a visible signal,
or rethrow with context).
- If an error appears while you work, find its cause. Do not wrap it to make it go away.
规则
Codex、Copilot 编码智能体等:加入 AGENTS.md
AGENTS.md## Don't hide errors
- Never add an empty catch, `except: pass`, or a catch that only logs and continues, unless the user asked
for it and you comment why the error is safe to ignore.
- Catch the narrowest error type you can handle, and handle it (retry, fallback with a visible signal,
or rethrow with context).
- If an error appears while you work, find its cause. Do not wrap it to make it go away.
Cursor 规则
Cursor:项目规则
.cursor/rules/swallowed-error.mdc---
description: "AgentFails: Errors swallowed to make it 'work'"
alwaysApply: true
---
## Don't hide errors
- Never add an empty catch, `except: pass`, or a catch that only logs and continues, unless the user asked
for it and you comment why the error is safe to ignore.
- Catch the narrowest error type you can handle, and handle it (retry, fallback with a visible signal,
or rethrow with context).
- If an error appears while you work, find its cause. Do not wrap it to make it go away.
CI / 脚本
能抓住它的 lint 规则(ESLint / Ruff)
eslint.config.mjs / pyproject.toml// eslint.config.mjs
rules: { "no-empty": ["error", { allowEmptyCatch: false }] }
# pyproject.toml (ruff)
[tool.ruff.lint]
extend-select = ["E722", "BLE001", "S110"] # bare except, blind except, try-except-pass