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

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

## 根因

路由返回了 HTTPException 实例而不是抛出它，导致 FastAPI 尝试将异常对象序列化为响应正文。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 0.100+ | active | — | — |

## 解决方案

1. **** (98% 成功率)
   ```
   raise HTTPException(status_code=404, detail='Not found')
   ```
2. **** (90% 成功率)
   ```
   from fastapi.responses import JSONResponse
return JSONResponse(status_code=404, content={'detail': 'Not found'})
   ```

## 无效尝试

- **** — Returns a 200 with exception internals in the body instead of an error status. (85% 失败率)
- **** — Leaks stack/details and still uses the wrong status code. (70% 失败率)
