From 54dec171f9fffdce5beddd12f746c2644c468a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Tue, 12 Mar 2024 14:12:24 +0100 Subject: [PATCH] fix: raise failed request to not run `mark_webhook_sent` (#2289) * fix: webhook sent raise a failed request is not raised even failed webhook would be marked is sent * add warn log * fix error * improve exceptions * fixup! * ConnectError is already captured by RequestError https://www.python-httpx.org/exceptions/ * log exception --- lnbits/core/tasks.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/lnbits/core/tasks.py b/lnbits/core/tasks.py index 2a6760734..46edd2653 100644 --- a/lnbits/core/tasks.py +++ b/lnbits/core/tasks.py @@ -51,7 +51,7 @@ async def killswitch_task(): "Switching to VoidWallet. Killswitch triggered." ) await switch_to_voidwallet() - except (httpx.ConnectError, httpx.RequestError): + except (httpx.RequestError, httpx.HTTPStatusError): logger.error( "Cannot fetch lnbits status manifest." f" {settings.lnbits_status_manifest}" @@ -121,9 +121,20 @@ async def wait_for_paid_invoices(invoice_paid_queue: asyncio.Queue): async with httpx.AsyncClient(headers=headers) as client: try: r = await client.post(url, timeout=4) - await mark_webhook_sent(payment, r.status_code) - except (httpx.ConnectError, httpx.RequestError): - pass + r.raise_for_status() + await mark_webhook_sent(payment.payment_hash, r.status_code) + except httpx.HTTPStatusError as exc: + status_code = exc.response.status_code + await mark_webhook_sent(payment.payment_hash, status_code) + logger.warning( + f"balance_notify returned a bad status_code: {status_code} " + f"while requesting {exc.request.url!r}." + ) + logger.warning(exc) + except httpx.RequestError as exc: + await mark_webhook_sent(payment.payment_hash, -1) + logger.warning(f"Could not send balance_notify to {url}") + logger.warning(exc) await send_payment_push_notification(payment) @@ -155,9 +166,17 @@ async def dispatch_webhook(payment: Payment): data = payment.dict() try: r = await client.post(payment.webhook, json=data, timeout=40) + r.raise_for_status() await mark_webhook_sent(payment.payment_hash, r.status_code) - except (httpx.ConnectError, httpx.RequestError): + except httpx.HTTPStatusError as exc: + await mark_webhook_sent(payment.payment_hash, exc.response.status_code) + logger.warning( + f"webhook returned a bad status_code: {exc.response.status_code} " + f"while requesting {exc.request.url!r}." + ) + except httpx.RequestError: await mark_webhook_sent(payment.payment_hash, -1) + logger.warning(f"Could not send webhook to {payment.webhook}") async def send_payment_push_notification(payment: Payment):