catch some errors on spark.

This commit is contained in:
fiatjaf
2021-03-27 21:24:08 -03:00
parent 574358a118
commit 3215b5d2bb

View File

@@ -1,7 +1,7 @@
import trio # type: ignore import trio # type: ignore
import random
import json import json
import httpx import httpx
import random
from os import getenv from os import getenv
from typing import Optional, AsyncGenerator from typing import Optional, AsyncGenerator
@@ -40,6 +40,7 @@ class SparkWallet(Wallet):
else: else:
params = {} params = {}
try:
async with httpx.AsyncClient() as client: async with httpx.AsyncClient() as client:
r = await client.post( r = await client.post(
self.url + "/rpc", self.url + "/rpc",
@@ -47,6 +48,8 @@ class SparkWallet(Wallet):
json={"method": key, "params": params}, json={"method": key, "params": params},
timeout=40, timeout=40,
) )
except (OSError, httpx.ConnectError, httpx.RequestError) as exc:
raise SparkError("error connecting to spark: " + str(exc))
try: try:
data = r.json() data = r.json()
@@ -143,7 +146,11 @@ class SparkWallet(Wallet):
return PaymentResponse(True, r["payment_hash"], fee_msat, preimage, None) return PaymentResponse(True, r["payment_hash"], fee_msat, preimage, None)
async def get_invoice_status(self, checking_id: str) -> PaymentStatus: async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
try:
r = await self.listinvoices(label=checking_id) r = await self.listinvoices(label=checking_id)
except (SparkError, UnknownError):
return PaymentStatus(None)
if not r or not r.get("invoices"): if not r or not r.get("invoices"):
return PaymentStatus(None) return PaymentStatus(None)
if r["invoices"][0]["status"] == "unpaid": if r["invoices"][0]["status"] == "unpaid":
@@ -160,7 +167,11 @@ class SparkWallet(Wallet):
return PaymentStatus(None) return PaymentStatus(None)
# ask sparko # ask sparko
try:
r = await self.listpays(payment_hash=checking_id) r = await self.listpays(payment_hash=checking_id)
except (SparkError, UnknownError):
return PaymentStatus(None)
if not r["pays"]: if not r["pays"]:
return PaymentStatus(False) return PaymentStatus(False)
if r["pays"][0]["payment_hash"] == checking_id: if r["pays"][0]["payment_hash"] == checking_id:
@@ -184,7 +195,7 @@ class SparkWallet(Wallet):
data = json.loads(line[5:]) data = json.loads(line[5:])
if "pay_index" in data and data.get("status") == "paid": if "pay_index" in data and data.get("status") == "paid":
yield data["label"] yield data["label"]
except (OSError, httpx.ReadError): except (OSError, httpx.ReadError, httpx.ConnectError):
pass pass
print("lost connection to spark /stream, retrying in 5 seconds") print("lost connection to spark /stream, retrying in 5 seconds")