plugins/languages/python/skills/error/SKILL.md
Python 异常处理与结构化日志规范。涵盖自定义异常层次、except* / ExceptionGroup、structlog 结构化日志、Context Manager 资源管理、错误传播策略。在设计异常类型、配置日志、排查异常、写资源清理代码时使用。也触发于"异常处理"、"自定义异常"、"structlog"、"logging"、"try/except"。
npx skillsauth add lazygophers/ccplugin python-errorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
每个项目定义一个根异常, 业务异常都继承自它:
class AppError(Exception):
"""应用根异常。所有业务异常继承自此。"""
class ValidationError(AppError):
def __init__(self, field: str, message: str) -> None:
super().__init__(f"{field}: {message}")
self.field = field
class NotFoundError(AppError):
def __init__(self, resource: str, id: int | str) -> None:
super().__init__(f"{resource} #{id} not found")
self.resource = resource
self.id = id
class ExternalServiceError(AppError):
"""外部服务调用失败 (可重试)。"""
好处:
except AppError 捕获全部业务异常except Exception 仅用于框架边界 (HTTP handler, async task 顶层)except: 或 except Exception: (除非顶层 handler)raise NewError(...) from e (用 from, 不要 raise NewError(str(e)))# good
try:
user = await db.get_user(uid)
except DatabaseConnectionError as e:
log.error("db_unavailable", user_id=uid, exc_info=True)
raise ExternalServiceError("user-service") from e
# bad - 吞异常
try:
user = await db.get_user(uid)
except Exception:
user = None # 静默失败, 上层无法察觉
并发任务 (TaskGroup, asyncio.gather) 会抛 ExceptionGroup, 必须用 except*:
async with asyncio.TaskGroup() as tg:
tg.create_task(fetch_a())
tg.create_task(fetch_b())
# 若两个都失败, raise ExceptionGroup([err_a, err_b])
try:
await run_pipeline()
except* ValidationError as eg:
for e in eg.exceptions:
log.warning("validation_failed", error=str(e))
except* ExternalServiceError as eg:
raise # 让上层重试
不要用 print 调试, 不要用裸 logging 拼字符串。用 structlog 输出 JSON:
import structlog
structlog.configure(
processors=[
structlog.contextvars.merge_contextvars,
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.JSONRenderer(),
],
)
log = structlog.get_logger()
# 业务代码: 键值对而非 f-string
log.info("user_created", user_id=user.id, email=user.email)
log.error("payment_failed", order_id=oid, amount=amt, exc_info=True)
关键约定:
snake_case, 动词过去式 (user_created 而非 creating user)structlog.contextvars.bind_contextvars(request_id=...) 注入, 不在每行手传exc_info=True 自动附带 traceback老项目仍用 stdlib logging 也可, 但配置 dictConfig + JSON formatter:
import logging
logger = logging.getLogger(__name__) # 不要 logging.getLogger() 拿 root
logger.error("payment failed for order=%s amount=%s", oid, amt, exc_info=True)
不要用 f-string 拼日志消息 (字符串会先求值, 即使日志级别被过滤掉)。用 %s 占位符。
任何需要清理的资源都用 with / async with, 不要手写 try/finally:
# sync
from contextlib import contextmanager
@contextmanager
def transaction(db):
tx = db.begin()
try:
yield tx
tx.commit()
except Exception:
tx.rollback()
raise
with transaction(db) as tx:
tx.execute(...)
# async
async with httpx.AsyncClient() as client:
resp = await client.get(url)
多资源用 contextlib.ExitStack / AsyncExitStack, 不要嵌套 with。
FastAPI / Litestar 等 Web 框架边界, 把业务异常转 HTTP 响应:
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
app = FastAPI()
@app.exception_handler(NotFoundError)
async def not_found_handler(req: Request, exc: NotFoundError) -> JSONResponse:
return JSONResponse(status_code=404, content={
"error": "not_found",
"resource": exc.resource,
"id": exc.id,
})
业务代码只 raise 领域异常, handler 统一翻译。不要在业务函数里 raise HTTPException。
except: pass / except Exception: pass (静默失败)raise Exception("...") (用具体异常类)logger.error(f"failed: {e}") (丢 traceback, 用 exc_info=True)print(...) 调试代码留在生产getLogger(__name__), 应用层配置)raise X from e)tools
--- name: trellisx-workspace description: 维护 `.trellis/task.md` 任务看板 —— trellis 缺的跨任务总览。**一个表格, 一行一个任务**, 列为 id/名称/描述/状态/阶段/进度/worktree (状态/阶段中文显示)。在 task create/start/阶段切换/archive 后**及时更新**对应行; 并**自动清理超 7 天的已完成行**防膨胀。保持看板与 task.json 实时一致。 when_to_use: 维护 / 创建 / 更新 `.trellis/task.md` 任务看板时; task 生命周期任一节点 (create/start/阶段推进/archive) 之后同步看板时; 用户问"当前有哪些任务 / 任务进度 / 任务看板"时。被 trellisx-flow 与 trellisx-apply 注入的流程引用。 user-invocable: true argument-hint: [show|update|sync|cleanup ...] [task id] arguments:
testing
强制以 Trellis task 闭环处理用户指定的请求 (自判新建/并入 → plan→exec→check→finish 全程不跳步)。**仅用户显式主动调用** (/trellisx-flow 或明确要求"强制走 task 处理这个"); **禁止自动 / 被动 / 推断式调用** —— 不要因为某个请求"看起来该建 task"就自动触发本 skill, 那是 apply 注入的 no_task 倾向的职责。
testing
把 强推task + subtask拆分 + worktree隔离 + 闭环收尾 四维度增量注入当前项目 .trellis/ (workflow.md 的 no_task/planning/in_progress 块 + spec 背书文档 + trellis 生命周期 hook worktree 自动化)。强推 task 与闭环为纯 prompt 软约束 (非平台 hook 硬拦截)。**纯增量追加, 绝不替换 trellis 原生文本** (no_task 分类+征同意/check/finish/前缀全保留)。幂等 (marker 包裹)。
development
Claude Code 会话历史整理 — 扫 ~/.claude/projects/**/*.jsonl 全部 session transcripts, 提取学习增量 (用户校正/决策/踩坑/L0 规则) → 全局记忆库 ~/.cortex/.wiki/memory/. 默认 --apply 落盘 (--dry-run opt-in 仅出 JSON plan 预览). 与 cortex-extract (L4-inbox 内部) 互补.