# TypeError: HTTPException 类型的对象无法 JSON 序列化

- **ID:** `python/fastapi-http-exception-detail-dict`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

路由将 HTTPException 对象返回或嵌入响应，而不是抛出它，导致 JSON 编码失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Raise it so FastAPI handles it:
raise HTTPException(status_code=404, detail='not found')
   ```
2. **** (85% 成功率)
   ```
   Return a plain dict with the error shape you want.
   ```

## 无效尝试

- **** — Serializes the exception but returns 200 with error data instead of the proper status. (75% 失败率)
- **** — Loses status code and structured detail. (70% 失败率)
