23 lines
690 B
Python
23 lines
690 B
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.responses import JSONResponse
|
|
|
|
|
|
def install_error_handlers(application: FastAPI) -> None:
|
|
@application.exception_handler(Exception)
|
|
async def unexpected_error(_request: Request, _error: Exception) -> JSONResponse:
|
|
correlation_id = uuid.uuid4().hex
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={
|
|
"error": {
|
|
"code": "internal_error",
|
|
"message": "服务暂时不可用,请稍后重试。",
|
|
"correlation_id": correlation_id,
|
|
}
|
|
},
|
|
)
|