Skip to content

Commit

Permalink
[FIX] Specs conformant error codes (#693)
Browse files Browse the repository at this point in the history
* error codes must follow spec

* make format
  • Loading branch information
lollerfirst authored Jan 21, 2025
1 parent 4e7917f commit ea96fab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
18 changes: 18 additions & 0 deletions cashu/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ class NotAllowedError(CashuError):
def __init__(self, detail: Optional[str] = None, code: Optional[int] = None):
super().__init__(detail or self.detail, code=code or self.code)

class OutputsAlreadySignedError(CashuError):
detail = "outputs have already been signed before."
code = 10002

def __init__(self, detail: Optional[str] = None, code: Optional[int] = None):
super().__init__(detail or self.detail, code=code or self.code)

class InvalidProofsError(CashuError):
detail = "proofs could not be verified"
code = 10003

def __init__(self, detail: Optional[str] = None, code: Optional[int] = None):
super().__init__(detail or self.detail, code=code or self.code)

class TransactionError(CashuError):
detail = "transaction error"
Expand Down Expand Up @@ -63,6 +76,11 @@ class TransactionUnitError(TransactionError):
def __init__(self, detail):
super().__init__(detail, code=self.code)

class TransactionAmountExceedsLimitError(TransactionError):
code = 11006

def __init__(self, detail):
super().__init__(detail, code=self.code)

class KeysetError(CashuError):
detail = "keyset error"
Expand Down
3 changes: 2 additions & 1 deletion cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
NotAllowedError,
QuoteNotPaidError,
QuoteSignatureInvalidError,
TransactionAmountExceedsLimitError,
TransactionError,
)
from ..core.helpers import sum_proofs
Expand Down Expand Up @@ -403,7 +404,7 @@ async def mint_quote(self, quote_request: PostMintQuoteRequest) -> MintQuote:
if not quote_request.amount > 0:
raise TransactionError("amount must be positive")
if settings.mint_max_peg_in and quote_request.amount > settings.mint_max_peg_in:
raise NotAllowedError(
raise TransactionAmountExceedsLimitError(
f"Maximum mint amount is {settings.mint_max_peg_in} sat."
)
if settings.mint_peg_out_only:
Expand Down
6 changes: 4 additions & 2 deletions cashu/mint/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
from ..core.crypto.secp import PublicKey
from ..core.db import Connection, Database
from ..core.errors import (
InvalidProofsError,
NoSecretInProofsError,
NotAllowedError,
OutputsAlreadySignedError,
SecretTooLongError,
TransactionError,
TransactionUnitError,
Expand Down Expand Up @@ -79,7 +81,7 @@ async def verify_inputs_and_outputs(
raise TransactionError("duplicate proofs.")
# Verify ecash signatures
if not all([self._verify_proof_bdhke(p) for p in proofs]):
raise TransactionError("could not verify proofs.")
raise InvalidProofsError()
# Verify input spending conditions
if not all([self._verify_input_spending_conditions(p) for p in proofs]):
raise TransactionError("validation of input spending conditions failed.")
Expand Down Expand Up @@ -141,7 +143,7 @@ async def _verify_outputs(
# verify that outputs have not been signed previously
signed_before = await self._check_outputs_issued_before(outputs, conn)
if any(signed_before):
raise TransactionError("outputs have already been signed before.")
raise OutputsAlreadySignedError()
logger.trace(f"Verified {len(outputs)} outputs.")

async def _check_outputs_issued_before(
Expand Down

0 comments on commit ea96fab

Please sign in to comment.