ln-772-error-handler-setup
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseln-772-error-handler-setup
ln-772-error-handler-setup
Type: L3 Worker
Category: 7XX Project Bootstrap
Parent: ln-770-crosscutting-setup
Configures global error handling for .NET and Python backend applications.
类型: L3 工作流
分类: 7XX 项目初始化
父节点: ln-770-crosscutting-setup
为.NET和Python后端应用配置全局错误处理。
Overview
概述
| Aspect | Details |
|---|---|
| Input | Context Store from ln-770 |
| Output | Exception handling middleware and custom exceptions |
| Stacks | .NET (ASP.NET Core Middleware), Python (FastAPI exception handlers) |
| 方面 | 详情 |
|---|---|
| 输入 | 来自ln-770的上下文存储 |
| 输出 | 异常处理中间件和自定义异常 |
| 技术栈 | .NET (ASP.NET Core Middleware), Python (FastAPI exception handlers) |
Phase 1: Receive Context
阶段1:接收上下文
Accept Context Store from coordinator.
Required Context:
- : .NET or Python
STACK - : ASP.NET Core or FastAPI
FRAMEWORK - : Project directory path
PROJECT_ROOT - : Development or Production
ENVIRONMENT
Idempotency Check:
- .NET: Grep for or
GlobalExceptionMiddlewareUseExceptionHandler - Python: Grep for or
@app.exception_handlerexception_handlers.py - If found: Return
{ "status": "skipped" }
接收来自协调器的上下文存储。
必填上下文:
- : .NET 或 Python
STACK - : ASP.NET Core 或 FastAPI
FRAMEWORK - : 项目目录路径
PROJECT_ROOT - : 开发环境或生产环境
ENVIRONMENT
幂等性检查:
- .NET: Grep for or
GlobalExceptionMiddlewareUseExceptionHandler - Python: Grep for or
@app.exception_handlerexception_handlers.py - 若已存在:返回
{ "status": "skipped" }
Phase 2: Research Error Handling Patterns
阶段2:研究错误处理模式
Use MCP tools to get up-to-date documentation.
For .NET:
MCP ref: "ASP.NET Core global exception handling middleware"
Context7: /dotnet/aspnetcoreFor Python:
MCP ref: "FastAPI exception handlers custom exceptions"
Context7: /tiangolo/fastapiKey Patterns to Research:
- Middleware pipeline positioning
- Exception type mapping to HTTP status codes
- ProblemDetails (RFC 7807) format
- Development vs Production error details
- Logging integration
使用MCP工具获取最新文档。
针对.NET:
MCP ref: "ASP.NET Core global exception handling middleware"
Context7: /dotnet/aspnetcore针对Python:
MCP ref: "FastAPI exception handlers custom exceptions"
Context7: /tiangolo/fastapi需研究的关键模式:
- 中间件管道定位
- 异常类型到HTTP状态码的映射
- ProblemDetails (RFC 7807) 格式
- 开发环境与生产环境的错误详情差异
- 日志集成
Phase 3: Decision Points
阶段3:决策点
Q1: Error Response Format
Q1:错误响应格式
| Option | Description |
|---|---|
| ProblemDetails (RFC 7807) (Recommended) | Standardized format, widely adopted |
| Custom Format | Project-specific requirements |
| 选项 | 描述 |
|---|---|
| ProblemDetails (RFC 7807)(推荐) | 标准化格式,被广泛采用 |
| 自定义格式 | 符合项目特定需求 |
Q2: Error Detail Level
Q2:错误详情级别
| Environment | Stack Trace | Inner Exceptions | Request Details |
|---|---|---|---|
| Development | ✓ Show | ✓ Show | ✓ Show |
| Production | ✗ Hide | ✗ Hide | ✗ Hide |
| 环境 | 堆栈跟踪 | 内部异常 | 请求详情 |
|---|---|---|---|
| 开发环境 | ✓ 显示 | ✓ 显示 | ✓ 显示 |
| 生产环境 | ✗ 隐藏 | ✗ 隐藏 | ✗ 隐藏 |
Q3: Error Taxonomy
Q3:错误分类
Define standard error codes:
| Code | HTTP Status | Description |
|---|---|---|
| 400 | Invalid input data |
| 401 | Authentication required |
| 403 | Insufficient permissions |
| 404 | Resource not found |
| 409 | Resource state conflict |
| 500 | Unexpected server error |
定义标准错误码:
| 代码 | HTTP状态码 | 描述 |
|---|---|---|
| 400 | 输入数据无效 |
| 401 | 需要身份验证 |
| 403 | 权限不足 |
| 404 | 资源未找到 |
| 409 | 资源状态冲突 |
| 500 | 服务器意外错误 |
Phase 4: Generate Configuration
阶段4:生成配置
.NET Output Files
.NET 输出文件
| File | Purpose |
|---|---|
| Exception handling middleware |
| Base exception class |
| Validation errors |
| Not found errors |
| Error response model |
Generation Process:
- Use MCP ref to get current ASP.NET Core exception handling patterns
- Generate GlobalExceptionMiddleware with:
- Exception type to HTTP status mapping
- Logging of exceptions
- ProblemDetails response format
- Environment-aware detail level
- Generate custom exception classes
Registration Code:
csharp
app.UseMiddleware<GlobalExceptionMiddleware>();| 文件 | 用途 |
|---|---|
| 异常处理中间件 |
| 基础异常类 |
| 验证错误类 |
| 资源未找到错误类 |
| 错误响应模型 |
生成流程:
- 使用MCP参考获取当前ASP.NET Core异常处理模式
- 生成GlobalExceptionMiddleware,包含:
- 异常类型到HTTP状态码的映射
- 异常日志记录
- ProblemDetails响应格式
- 感知环境的详情级别
- 生成自定义异常类
注册代码:
csharp
app.UseMiddleware<GlobalExceptionMiddleware>();Python Output Files
Python 输出文件
| File | Purpose |
|---|---|
| Custom exception classes |
| FastAPI exception handlers |
| Pydantic error models |
Generation Process:
- Use MCP ref to get current FastAPI exception handling patterns
- Generate exception handlers with:
- HTTPException handling
- Custom AppException handling
- Validation error handling
- Request validation error handling
- Generate custom exception classes
Registration Code:
python
app.add_exception_handler(AppException, app_exception_handler)
app.add_exception_handler(RequestValidationError, validation_exception_handler)| 文件 | 用途 |
|---|---|
| 自定义异常类 |
| FastAPI异常处理器 |
| Pydantic错误模型 |
生成流程:
- 使用MCP参考获取当前FastAPI异常处理模式
- 生成异常处理器,包含:
- HTTPException处理
- 自定义AppException处理
- 验证错误处理
- 请求验证错误处理
- 生成自定义异常类
注册代码:
python
app.add_exception_handler(AppException, app_exception_handler)
app.add_exception_handler(RequestValidationError, validation_exception_handler)Phase 5: Validate
阶段5:验证
Validation Steps:
-
Syntax check:
- .NET:
dotnet build --no-restore - Python:
python -m py_compile exceptions/handlers.py
- .NET:
-
Test error handling:
- Create test endpoint that throws exception
- Verify error response format
- Check that stack trace hidden in Production
Expected Error Response (ProblemDetails):
json
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "Validation Error",
"status": 400,
"detail": "Invalid input data",
"instance": "/api/users",
"errors": [
{ "field": "email", "message": "Invalid email format" }
],
"traceId": "abc-123-def-456"
}验证步骤:
-
语法检查:
- .NET:
dotnet build --no-restore - Python:
python -m py_compile exceptions/handlers.py
- .NET:
-
测试错误处理:
- 创建抛出异常的测试端点
- 验证错误响应格式
- 检查生产环境中堆栈跟踪是否被隐藏
预期错误响应(ProblemDetails格式):
json
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "Validation Error",
"status": 400,
"detail": "Invalid input data",
"instance": "/api/users",
"errors": [
{ "field": "email", "message": "Invalid email format" }
],
"traceId": "abc-123-def-456"
}Return to Coordinator
返回协调器
json
{
"status": "success",
"files_created": [
"Middleware/GlobalExceptionMiddleware.cs",
"Exceptions/AppException.cs",
"Models/ErrorResponse.cs"
],
"packages_added": [],
"registration_code": "app.UseMiddleware<GlobalExceptionMiddleware>();",
"message": "Configured global exception handling"
}json
{
"status": "success",
"files_created": [
"Middleware/GlobalExceptionMiddleware.cs",
"Exceptions/AppException.cs",
"Models/ErrorResponse.cs"
],
"packages_added": [],
"registration_code": "app.UseMiddleware<GlobalExceptionMiddleware>();",
"message": "Configured global exception handling"
}Reference Links
参考链接
Version: 2.0.0
Last Updated: 2026-01-10