fix: fastapi exception handling and printing
This commit is contained in:
parent
cc60e11226
commit
f47772d528
|
@ -68,28 +68,6 @@ def create_app(config_object="lnbits.settings") -> FastAPI:
|
||||||
g().config = lnbits.settings
|
g().config = lnbits.settings
|
||||||
g().base_url = f"http://{lnbits.settings.HOST}:{lnbits.settings.PORT}"
|
g().base_url = f"http://{lnbits.settings.HOST}:{lnbits.settings.PORT}"
|
||||||
|
|
||||||
@app.exception_handler(RequestValidationError)
|
|
||||||
async def validation_exception_handler(
|
|
||||||
request: Request, exc: RequestValidationError
|
|
||||||
):
|
|
||||||
# Only the browser sends "text/html" request
|
|
||||||
# not fail proof, but everything else get's a JSON response
|
|
||||||
|
|
||||||
if (
|
|
||||||
request.headers
|
|
||||||
and "accept" in request.headers
|
|
||||||
and "text/html" in request.headers["accept"]
|
|
||||||
):
|
|
||||||
return template_renderer().TemplateResponse(
|
|
||||||
"error.html",
|
|
||||||
{"request": request, "err": f"{exc.errors()} is not a valid UUID."},
|
|
||||||
)
|
|
||||||
|
|
||||||
return JSONResponse(
|
|
||||||
status_code=HTTPStatus.NO_CONTENT,
|
|
||||||
content={"detail": exc.errors()},
|
|
||||||
)
|
|
||||||
|
|
||||||
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
||||||
|
|
||||||
check_funding_source(app)
|
check_funding_source(app)
|
||||||
|
@ -192,25 +170,22 @@ def register_async_tasks(app):
|
||||||
|
|
||||||
def register_exception_handlers(app: FastAPI):
|
def register_exception_handlers(app: FastAPI):
|
||||||
@app.exception_handler(Exception)
|
@app.exception_handler(Exception)
|
||||||
async def basic_error(request: Request, err):
|
async def exception_handler(request: Request, exc: Exception):
|
||||||
logger.error("handled error", traceback.format_exc())
|
|
||||||
logger.error("ERROR:", err)
|
|
||||||
etype, _, tb = sys.exc_info()
|
etype, _, tb = sys.exc_info()
|
||||||
traceback.print_exception(etype, err, tb)
|
traceback.print_exception(etype, exc, tb)
|
||||||
exc = traceback.format_exc()
|
exc_str = str(exc)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
request.headers
|
request.headers
|
||||||
and "accept" in request.headers
|
and "accept" in request.headers
|
||||||
and "text/html" in request.headers["accept"]
|
and "text/html" in request.headers["accept"]
|
||||||
):
|
):
|
||||||
return template_renderer().TemplateResponse(
|
return template_renderer().TemplateResponse(
|
||||||
"error.html", {"request": request, "err": err}
|
"error.html", {"request": request, "err": exc_str}
|
||||||
)
|
)
|
||||||
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=HTTPStatus.NO_CONTENT,
|
status_code=HTTPStatus.BAD_REQUEST,
|
||||||
content={"detail": err},
|
content={"detail": exc_str},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user