Error Handling(错误处理)
知识库位置:
FDE Knowledge Base/03 Engineering/Software Engineering/Code Quality/Error-Handling.md
真实客户环境中,系统一定会失败。
因此工程问题不是:
如何让系统永远不出错?
而是:
出错之后系统会发生什么?
Common Failure Types(常见失败类型)
Section titled “Common Failure Types(常见失败类型)”User Error(用户错误)
Section titled “User Error(用户错误)”例如:
- 参数错误
- 文件格式不支持
- 缺少必要信息
Dependency Error(依赖错误)
Section titled “Dependency Error(依赖错误)”例如:
- CRM API unavailable(客户关系管理系统接口不可用)
- Database timeout(数据库超时)
- LLM API failure(大语言模型接口失败)
Permission Error(权限错误)
Section titled “Permission Error(权限错误)”例如:
- Unauthorized(未认证)
- Forbidden(已认证但无权限)
System Error(系统错误)
Section titled “System Error(系统错误)”例如:
- Service crash(服务崩溃)
- Out of memory(内存耗尽)
- Internal exception(内部异常)
Good Error Handling(好的错误处理)
Section titled “Good Error Handling(好的错误处理)”错误应该至少包含:
Error Type(错误类型)Error Code(错误码)User-safe Message(用户安全提示)Internal Details(内部细节)Trace ID(链路 ID)例如:
ORDER_SERVICE_TIMEOUT(订单服务超时)不要只返回:
Something went wrong(出了点问题)Retry(重试)
Section titled “Retry(重试)”适合 Retry(重试)的错误:
- Temporary network failure(临时网络故障)
- Rate limit(限流)
- Transient dependency issue(瞬态依赖问题)
不适合 Retry(重试)的:
- Invalid input(非法输入)
- Permission denied(权限拒绝)
- Business rule violation(违反业务规则)
FDE Principle(FDE 原则)
Section titled “FDE Principle(FDE 原则)”不要:
Retry Everything(重试一切)应该:
Classify Error(错误分类)↓Determine Retryability(判定可重试性)↓Retry / Fail / Escalate(重试/失败/升级处理)