فهرست منبع

fix: error mapping and fall back to unknown (#1518)

tsk 2 هفته پیش
والد
کامیت
b1cb48016e

+ 93 - 3
crates/cdk-common/src/error.rs

@@ -637,12 +637,102 @@ impl From<Error> for ErrorResponse {
                 code: ErrorCode::InvoiceAlreadyPaid,
                 detail: "Invoice already paid or pending".to_string(),
             },
-            // Fallback: use TokenNotVerified (10001) for unhandled errors
-            // as it's the most generic verification failure code in the spec
-            _ => ErrorResponse {
+
+            // DHKE errors - TokenNotVerified for actual verification failures
+            Error::DHKE(crate::dhke::Error::TokenNotVerified) => ErrorResponse {
+                code: ErrorCode::TokenNotVerified,
+                detail: err.to_string(),
+            },
+            Error::DHKE(_) => ErrorResponse {
+                code: ErrorCode::Unknown(50000),
+                detail: err.to_string(),
+            },
+
+            // Verification errors
+            Error::CouldNotVerifyDleq => ErrorResponse {
                 code: ErrorCode::TokenNotVerified,
                 detail: err.to_string(),
             },
+            Error::SignatureMissingOrInvalid => ErrorResponse {
+                code: ErrorCode::WitnessMissingOrInvalid,
+                detail: err.to_string(),
+            },
+            Error::SigAllUsedInMelt => ErrorResponse {
+                code: ErrorCode::WitnessMissingOrInvalid,
+                detail: err.to_string(),
+            },
+
+            // Keyset/key errors
+            Error::AmountKey => ErrorResponse {
+                code: ErrorCode::KeysetNotFound,
+                detail: err.to_string(),
+            },
+            Error::KeysetUnknown(_) => ErrorResponse {
+                code: ErrorCode::KeysetNotFound,
+                detail: err.to_string(),
+            },
+            Error::NoActiveKeyset => ErrorResponse {
+                code: ErrorCode::KeysetInactive,
+                detail: err.to_string(),
+            },
+
+            // Quote/payment errors
+            Error::UnknownQuote => ErrorResponse {
+                code: ErrorCode::Unknown(50000),
+                detail: err.to_string(),
+            },
+            Error::MeltingDisabled => ErrorResponse {
+                code: ErrorCode::MintingDisabled,
+                detail: err.to_string(),
+            },
+            Error::PaymentPending => ErrorResponse {
+                code: ErrorCode::QuotePending,
+                detail: err.to_string(),
+            },
+            Error::UnknownPaymentState => ErrorResponse {
+                code: ErrorCode::Unknown(50000),
+                detail: err.to_string(),
+            },
+
+            // Transaction/amount errors
+            Error::SplitValuesGreater => ErrorResponse {
+                code: ErrorCode::TransactionUnbalanced,
+                detail: err.to_string(),
+            },
+            Error::AmountOverflow => ErrorResponse {
+                code: ErrorCode::TransactionUnbalanced,
+                detail: err.to_string(),
+            },
+            Error::OverIssue => ErrorResponse {
+                code: ErrorCode::TransactionUnbalanced,
+                detail: err.to_string(),
+            },
+
+            // Invoice parsing errors - no spec code for invalid format
+            Error::InvalidPaymentRequest => ErrorResponse {
+                code: ErrorCode::Unknown(50000),
+                detail: err.to_string(),
+            },
+            Error::InvoiceAmountUndefined => ErrorResponse {
+                code: ErrorCode::AmountlessInvoiceNotSupported,
+                detail: err.to_string(),
+            },
+
+            // Internal/system errors - use Unknown(99999)
+            Error::Internal => ErrorResponse {
+                code: ErrorCode::Unknown(50000),
+                detail: err.to_string(),
+            },
+            Error::Database(_) => ErrorResponse {
+                code: ErrorCode::Unknown(50000),
+                detail: err.to_string(),
+            },
+
+            // Fallback for any remaining errors - use Unknown(99999) instead of TokenNotVerified
+            _ => ErrorResponse {
+                code: ErrorCode::Unknown(50000),
+                detail: err.to_string(),
+            },
         }
     }
 }

+ 1 - 1
crates/cdk/src/wallet/issue/bolt11.rs

@@ -246,7 +246,7 @@ impl Wallet {
 
         let unix_time = unix_time();
 
-        if quote_info.expiry > unix_time {
+        if quote_info.expiry < unix_time && quote_info.expiry != 0 {
             tracing::warn!("Attempting to mint with expired quote.");
         }
 

+ 1 - 1
crates/cdk/src/wallet/issue/bolt12.rs

@@ -93,7 +93,7 @@ impl Wallet {
         let quote_info = self.localstore.get_mint_quote(quote_id).await?;
 
         let quote_info = if let Some(quote) = quote_info {
-            if quote.expiry.le(&unix_time()) && quote.expiry.ne(&0) {
+            if quote.expiry < unix_time() && quote.expiry != 0 {
                 tracing::info!("Attempting to mint expired quote.");
             }
 

+ 1 - 1
crates/cdk/src/wallet/issue/custom.rs

@@ -113,7 +113,7 @@ impl Wallet {
 
         let unix_time = unix_time();
 
-        if quote_info.expiry > unix_time {
+        if quote_info.expiry < unix_time && quote_info.expiry != 0 {
             tracing::warn!("Attempting to mint with expired quote.");
         }
 

+ 1 - 1
crates/cdk/src/wallet/issue/issue_bolt11.rs

@@ -242,7 +242,7 @@ impl Wallet {
 
         let unix_time = unix_time();
 
-        if quote_info.expiry > unix_time {
+        if quote_info.expiry < unix_time && quote_info.expiry != 0 {
             tracing::warn!("Attempting to mint with expired quote.");
         }
 

+ 1 - 1
crates/cdk/src/wallet/issue/issue_bolt12.rs

@@ -89,7 +89,7 @@ impl Wallet {
         let quote_info = self.localstore.get_mint_quote(quote_id).await?;
 
         let quote_info = if let Some(quote) = quote_info {
-            if quote.expiry.le(&unix_time()) && quote.expiry.ne(&0) {
+            if quote.expiry < unix_time() && quote.expiry != 0 {
                 tracing::info!("Attempting to mint expired quote.");
             }