add custom error handler for lnurl

This commit is contained in:
callebtc 2023-08-28 16:13:59 +02:00
commit 1072db72c1

View file

@ -1,8 +1,12 @@
from fastapi import APIRouter from fastapi import APIRouter, Request, Response
from fastapi.routing import APIRoute
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.responses import JSONResponse
from lnbits.db import Database from lnbits.db import Database
from lnbits.helpers import template_renderer from lnbits.helpers import template_renderer
from typing import Callable
db = Database("ext_withdraw") db = Database("ext_withdraw")
@ -15,7 +19,29 @@ withdraw_static_files = [
] ]
class LNURLErrorResponseHandler(APIRoute):
def get_route_handler(self) -> Callable:
original_route_handler = super().get_route_handler()
async def custom_route_handler(request: Request) -> Response:
try:
response = await original_route_handler(request)
except HTTPException as exc:
logger.debug(f"HTTPException: {exc}")
response = JSONResponse(
status_code=exc.status_code,
content={"status": "ERROR", "reason": f"{exc.detail}"},
)
except Exception as exc:
raise exc
return response
return custom_route_handler
withdraw_ext: APIRouter = APIRouter(prefix="/withdraw", tags=["withdraw"]) withdraw_ext: APIRouter = APIRouter(prefix="/withdraw", tags=["withdraw"])
withdraw_ext.route_class = LNURLErrorResponseHandler
def withdraw_renderer(): def withdraw_renderer():