diff --git a/__init__.py b/__init__.py index cb5eb9c..c9e0eb8 100644 --- a/__init__.py +++ b/__init__.py @@ -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.responses import JSONResponse from lnbits.db import Database from lnbits.helpers import template_renderer +from typing import Callable 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.route_class = LNURLErrorResponseHandler def withdraw_renderer():