home / skills / forge-town / skills / check-try-catch

check-try-catch skill

/skills/check-try-catch

This skill analyzes TypeScript and Python try-catch blocks to detect empty or purely logging catches and suggests concrete fixes.

npx playbooks add skill forge-town/skills --skill check-try-catch

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
1.8 KB
---
name: check-try-catch
description: 提供代码审查指南,识别并修复空catch块和纯console日志catch块问题
dependency:
  python: []
  system: []
---

# Check Try-Catch 代码审查指南

## 任务目标
- 识别代码中的空 catch 块(没有实际处理逻辑)
- 识别 catch 块中只有 console.log/print 等日志语句
- 提供修复建议和最佳实践
- 支持多语言代码审查

## 前置准备
无特殊依赖,本指南适用于人工代码审查或智能体辅助审查。

## 操作步骤

### 标准流程

1. **识别问题代码模式**
   - 搜索代码中的 try-catch 或 try-except 结构
   - 检查 catch/except 块的内容

2. **评估问题严重性**
   - 空 catch:高严重性,错误被完全忽略
   - 纯 console 日志:中严重性,错误被记录但未处理

3. **应用修复策略**
   - 参考 [references/patterns.md](references/patterns.md) 中的修复模式
   - 根据具体场景选择合适的处理方式

4. **验证修复结果**
   - 确保每个 catch/except 块都有明确的错误处理意图

## 资源索引
- 模式参考: [references/patterns.md](references/patterns.md) (何时读取:需要具体的错误示例、修复策略和检查清单时)

## 注意事项
- 本指南适用于 JavaScript/TypeScript 和 Python 代码
- 代码审查应结合业务上下文判断错误的处理策略
- 建议在代码评审阶段执行此检查
- 可以配合静态分析工具自动化检测

## 使用示例

**场景 1:人工代码审查**
```bash
# 在代码评审时,按照本指南检查提交的代码
# 重点关注新增或修改的 try-catch 块
```

**场景 2:智能体辅助审查**
```
请按照 check-try-catch 指南审查以下代码:
[粘贴代码]

智能体将:
1. 识别所有 try-catch 结构
2. 检测空 catch 和纯 console 日志 catch
3. 提供具体的修复建议
```

Overview

This skill provides a practical code review guide that detects empty catch blocks and catch blocks that only log errors (e.g., console.log). It targets TypeScript/JavaScript and Python patterns and delivers concrete remediation suggestions to ensure errors are handled intentionally. The goal is to reduce silent failures and improve observability and reliability in codebases.

How this skill works

The skill scans source for try-catch / try-except structures and inspects the catch/except body to classify issues as empty, logging-only, or appropriately handled. For each finding it returns severity, a short rationale, and a recommended fix pattern drawn from established remediation strategies. It can be used manually during code review or integrated with automated agents and static analysis workflows.

When to use it

  • During pull request or code review to catch risky error-handling patterns
  • As part of CI checks to flag empty or logging-only catches before merge
  • When hardening an application to improve reliability and observability
  • When onboarding developers to consistent error-handling practices
  • During refactoring to replace unsafe catch patterns with explicit handling

Best practices

  • Treat empty catch blocks as high severity; do not silently swallow errors
  • Avoid catch blocks that only call console.log; either handle or rethrow
  • Prefer contextual handling: recover, retry, compensate, or rethrow with context
  • Add structured logging and correlation data when errors are recorded
  • Use domain-specific actions (fallbacks, metrics, user-friendly messages) not just logs

Example use cases

  • Review a PR that introduced a try/catch to ensure the catch performs meaningful work
  • Run as an automated agent on affected files to produce a list of fix suggestions
  • Educate team members by showing examples of empty vs. handled catch blocks
  • Integrate with linter or CI to enforce non-empty, non-logging-only catches
  • Audit legacy code for silent failures and propose prioritized remediation

FAQ

Which languages are supported?

Primary support is for JavaScript/TypeScript and Python patterns; the approach generalizes to other C-style try/catch languages with minor adaptation.

What remediation suggestions does the skill provide?

It suggests rethrowing with context, adding concrete recovery or fallback logic, recording structured telemetry, or escalating the error when appropriate.