# 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": "服务器错误"
        }
    )
```
