> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/python/fastapi/fastapi-chu-li-tortoiseorm-yi-chang.md).

# fastapi处理tortoise-orm异常

### fastapi处理tortoise-orm异常

1. 将add\_exception\_handlers设置为false

```python
register_tortoise(
                  add_exception_handlers=False)
```

2. 捕捉异常

```python
@app.exception_handler(BaseORMException)
async def business_exception_handler(request: Request, error: BaseORMException):
    return JSONResponse(
        status_code=status.HTTP_200_OK,
        content={
            "code": status.HTTP_500_INTERNAL_SERVER_ERROR,
            "message": "服务器错误"
        }
    )
```
