feat: mask unexcepted error and add a exception id (#3178)

This commit is contained in:
dni ⚡ 2025-05-27 11:37:38 +02:00 committed by GitHub
parent 3c4d186dba
commit 4071925f65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,6 +7,7 @@ from fastapi import FastAPI, HTTPException, Request
from fastapi.exceptions import RequestValidationError from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse, RedirectResponse, Response from fastapi.responses import JSONResponse, RedirectResponse, Response
from loguru import logger from loguru import logger
from shortuuid import uuid
from lnbits.settings import settings from lnbits.settings import settings
@ -71,10 +72,11 @@ def register_exception_handlers(app: FastAPI):
async def exception_handler(request: Request, exc: Exception): async def exception_handler(request: Request, exc: Exception):
etype, _, tb = sys.exc_info() etype, _, tb = sys.exc_info()
traceback.print_exception(etype, exc, tb) traceback.print_exception(etype, exc, tb)
logger.error(f"Exception: {exc!s}") exception_id = uuid()
logger.error(f"Exception ID: {exception_id}\n{exc!s}")
return render_html_error(request, exc) or JSONResponse( return render_html_error(request, exc) or JSONResponse(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
content={"detail": str(exc)}, content={"detail": f"Unexpected error! ID: {exception_id}"},
) )
@app.exception_handler(AssertionError) @app.exception_handler(AssertionError)