Add LightningPaymentFailedError exception (#706)

* add error code

* fix test
This commit is contained in:
callebtc
2025-03-06 00:04:37 +01:00
committed by GitHub
parent f72a3f260f
commit 9305905a85
3 changed files with 19 additions and 10 deletions

View File

@@ -153,6 +153,14 @@ class QuoteNotPaidError(CashuError):
super().__init__(self.detail, code=self.code) super().__init__(self.detail, code=self.code)
class LightningPaymentFailedError(CashuError):
detail = "Lightning payment failed"
code = 20004
def __init__(self, detail: Optional[str] = None):
super().__init__(detail or self.detail, code=self.code)
class QuoteSignatureInvalidError(CashuError): class QuoteSignatureInvalidError(CashuError):
detail = "Signature for mint request invalid" detail = "Signature for mint request invalid"
code = 20008 code = 20008

View File

@@ -36,6 +36,7 @@ from ..core.errors import (
KeysetError, KeysetError,
KeysetNotFoundError, KeysetNotFoundError,
LightningError, LightningError,
LightningPaymentFailedError,
NotAllowedError, NotAllowedError,
QuoteNotPaidError, QuoteNotPaidError,
QuoteSignatureInvalidError, QuoteSignatureInvalidError,
@@ -1057,7 +1058,7 @@ class Ledger(LedgerVerification, LedgerSpendingConditions, LedgerTasks, LedgerFe
logger.error( logger.error(
f"Status check error: {status.error_message}" f"Status check error: {status.error_message}"
) )
raise LightningError( raise LightningPaymentFailedError(
f"Lightning payment failed{': ' + payment.error_message if payment.error_message else ''}." f"Lightning payment failed{': ' + payment.error_message if payment.error_message else ''}."
) )
case _: case _:

View File

@@ -4,7 +4,7 @@ import pytest
import pytest_asyncio import pytest_asyncio
from cashu.core.base import MeltQuote, MeltQuoteState, Proof from cashu.core.base import MeltQuote, MeltQuoteState, Proof
from cashu.core.errors import LightningError from cashu.core.errors import LightningPaymentFailedError
from cashu.core.models import PostMeltQuoteRequest, PostMintQuoteRequest from cashu.core.models import PostMeltQuoteRequest, PostMintQuoteRequest
from cashu.core.settings import settings from cashu.core.settings import settings
from cashu.lightning.base import PaymentResult from cashu.lightning.base import PaymentResult
@@ -220,32 +220,32 @@ async def test_melt_lightning_pay_invoice_failed_failed(ledger: Ledger, wallet:
settings.fakewallet_pay_invoice_state = PaymentResult.FAILED.name settings.fakewallet_pay_invoice_state = PaymentResult.FAILED.name
try: try:
await ledger.melt(proofs=wallet.proofs, quote=quote_id) await ledger.melt(proofs=wallet.proofs, quote=quote_id)
raise AssertionError("Expected LightningError") raise AssertionError("Expected LightningPaymentFailedError")
except LightningError: except LightningPaymentFailedError:
pass pass
settings.fakewallet_payment_state = PaymentResult.UNKNOWN.name settings.fakewallet_payment_state = PaymentResult.UNKNOWN.name
settings.fakewallet_pay_invoice_state = PaymentResult.FAILED.name settings.fakewallet_pay_invoice_state = PaymentResult.FAILED.name
try: try:
await ledger.melt(proofs=wallet.proofs, quote=quote_id) await ledger.melt(proofs=wallet.proofs, quote=quote_id)
raise AssertionError("Expected LightningError") raise AssertionError("Expected LightningPaymentFailedError")
except LightningError: except LightningPaymentFailedError:
pass pass
settings.fakewallet_payment_state = PaymentResult.FAILED.name settings.fakewallet_payment_state = PaymentResult.FAILED.name
settings.fakewallet_pay_invoice_state = PaymentResult.UNKNOWN.name settings.fakewallet_pay_invoice_state = PaymentResult.UNKNOWN.name
try: try:
await ledger.melt(proofs=wallet.proofs, quote=quote_id) await ledger.melt(proofs=wallet.proofs, quote=quote_id)
raise AssertionError("Expected LightningError") raise AssertionError("Expected LightningPaymentFailedError")
except LightningError: except LightningPaymentFailedError:
pass pass
settings.fakewallet_payment_state = PaymentResult.UNKNOWN.name settings.fakewallet_payment_state = PaymentResult.UNKNOWN.name
settings.fakewallet_pay_invoice_state = PaymentResult.UNKNOWN.name settings.fakewallet_pay_invoice_state = PaymentResult.UNKNOWN.name
try: try:
await ledger.melt(proofs=wallet.proofs, quote=quote_id) await ledger.melt(proofs=wallet.proofs, quote=quote_id)
raise AssertionError("Expected LightningError") raise AssertionError("Expected LightningPaymentFailedError")
except LightningError: except LightningPaymentFailedError:
pass pass