From 1072db72c14d59d16760e79d0af2abc8874b2544 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Mon, 28 Aug 2023 16:13:59 +0200 Subject: [PATCH] add custom error handler for lnurl --- __init__.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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():