fix: gracefully handle Lnurl errors on api (#123)

* fix: gracefully handle InvalidLnurl error on api

closes #122
This commit is contained in:
dni ⚡ 2026-01-15 09:09:21 +01:00 committed by GitHub
commit 76c5841bc8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -10,6 +10,7 @@ from lnbits.decorators import (
require_admin_key, require_admin_key,
require_invoice_key, require_invoice_key,
) )
from lnurl import InvalidUrl
from .crud import ( from .crud import (
create_pay_link, create_pay_link,
@ -28,6 +29,24 @@ from .models import CreatePayLinkData, LnurlpSettings, PayLink
lnurlp_api_router = APIRouter() lnurlp_api_router = APIRouter()
def check_lnurl_encode(req: Request, link_id: str) -> str:
try:
return lnurl_encode_link_id(req, link_id)
except InvalidUrl as exc:
raise HTTPException(
detail=(
f"Invalid URL for LNURL encoding: `{req.base_url}`. "
"Check proxy settings."
),
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
) from exc
except Exception as exc:
raise HTTPException(
detail="Error encoding LNURL.",
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
) from exc
@lnurlp_api_router.get("/api/v1/links", status_code=HTTPStatus.OK) @lnurlp_api_router.get("/api/v1/links", status_code=HTTPStatus.OK)
async def api_links( async def api_links(
req: Request, req: Request,
@ -41,7 +60,7 @@ async def api_links(
links = await get_pay_links(wallet_ids) links = await get_pay_links(wallet_ids)
for link in links: for link in links:
link.lnurl = lnurl_encode_link_id(req=req, link_id=link.id) link.lnurl = check_lnurl_encode(req=req, link_id=link.id)
return links return links
@ -66,7 +85,7 @@ async def api_link_retrieve(
detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN detail="Not your pay link.", status_code=HTTPStatus.FORBIDDEN
) )
link.lnurl = lnurl_encode_link_id(req, link.id) link.lnurl = check_lnurl_encode(req, link.id)
return link return link
@ -178,7 +197,8 @@ async def api_link_create_or_update(
link = await create_pay_link(data) link = await create_pay_link(data)
link.lnurl = lnurl_encode_link_id(req, link.id) link.lnurl = check_lnurl_encode(req=req, link_id=link.id)
return link return link