Kaynağa Gözat

Remove unwrap from cdk-sql-common

Cesar Rodas 3 ay önce
ebeveyn
işleme
69c71fc7f5

+ 1 - 0
crates/cdk-sql-common/src/macros.rs

@@ -5,6 +5,7 @@
 #[macro_export]
 macro_rules! unpack_into {
     (let ($($var:ident),+) = $array:expr) => {
+        #[allow(unused_parens)]
         let ($($var),+) = {
             let mut vec = $array.to_vec();
             vec.reverse();

+ 4 - 3
crates/cdk-sql-common/src/mint/mod.rs

@@ -135,7 +135,6 @@ where
 {
     type Err = Error;
 
-    // FIXME: Replace unwrap with proper error handling
     async fn add_proofs(
         &mut self,
         proofs: Proofs,
@@ -180,7 +179,7 @@ where
             .bind("c", proof.c.to_bytes().to_vec())
             .bind(
                 "witness",
-                proof.witness.map(|w| serde_json::to_string(&w).unwrap()),
+                proof.witness.and_then(|w| serde_json::to_string(&w).inspect_err(|e| tracing::error!("Failed to serialize witness: {:?}", e)).ok()),
             )
             .bind("state", "UNSPENT".to_string())
             .bind("quote_id", quote_id.clone().map(|q| q.to_string()))
@@ -2521,7 +2520,9 @@ fn sql_row_to_melt_quote(row: Vec<Column>) -> Result<mint::MeltQuote, Error> {
                 "Melt quote from pre migrations defaulting to bolt11 {}.",
                 err
             );
-            let bolt11 = Bolt11Invoice::from_str(&request).unwrap();
+            let bolt11 = Bolt11Invoice::from_str(&request).map_err(|e| {
+                Error::Internal(format!("Could not parse invoice: {}", e.to_string()))
+            })?;
             MeltPaymentRequest::Bolt11 { bolt11 }
         }
     };

+ 36 - 8
crates/cdk-sql-common/src/stmt.rs

@@ -72,7 +72,6 @@ pub enum SqlParseError {
 ///
 /// This function does not validate the SQL statement, it only extracts the placeholder to be
 /// database agnostic.
-// FIXME: Replace unwraps with proper error handling
 pub fn split_sql_parts(input: &str) -> Result<Vec<SqlPart>, SqlParseError> {
     let mut parts = Vec::new();
     let mut current = String::new();
@@ -83,16 +82,28 @@ pub fn split_sql_parts(input: &str) -> Result<Vec<SqlPart>, SqlParseError> {
             '\'' | '"' => {
                 // Start of string literal
                 let quote = c;
-                current.push(chars.next().unwrap());
+                current.push(
+                    chars
+                        .next()
+                        .ok_or(SqlParseError::UnterminatedStringLiteral)?,
+                );
 
                 let mut closed = false;
                 while let Some(&next) = chars.peek() {
-                    current.push(chars.next().unwrap());
+                    current.push(
+                        chars
+                            .next()
+                            .ok_or(SqlParseError::UnterminatedStringLiteral)?,
+                    );
 
                     if next == quote {
                         if chars.peek() == Some(&quote) {
                             // Escaped quote (e.g. '' inside strings)
-                            current.push(chars.next().unwrap());
+                            current.push(
+                                chars
+                                    .next()
+                                    .ok_or(SqlParseError::UnterminatedStringLiteral)?,
+                            );
                         } else {
                             closed = true;
                             break;
@@ -106,10 +117,19 @@ pub fn split_sql_parts(input: &str) -> Result<Vec<SqlPart>, SqlParseError> {
             }
 
             '-' => {
-                current.push(chars.next().unwrap());
+                current.push(
+                    chars
+                        .next()
+                        .ok_or(SqlParseError::UnterminatedStringLiteral)?,
+                );
+
                 if chars.peek() == Some(&'-') {
                     while let Some(&next) = chars.peek() {
-                        current.push(chars.next().unwrap());
+                        current.push(
+                            chars
+                                .next()
+                                .ok_or(SqlParseError::UnterminatedStringLiteral)?,
+                        );
                         if next == '\n' {
                             break;
                         }
@@ -129,7 +149,11 @@ pub fn split_sql_parts(input: &str) -> Result<Vec<SqlPart>, SqlParseError> {
 
                 while let Some(&next) = chars.peek() {
                     if next.is_alphanumeric() || next == '_' {
-                        name.push(chars.next().unwrap());
+                        name.push(
+                            chars
+                                .next()
+                                .ok_or(SqlParseError::UnterminatedStringLiteral)?,
+                        );
                     } else {
                         break;
                     }
@@ -143,7 +167,11 @@ pub fn split_sql_parts(input: &str) -> Result<Vec<SqlPart>, SqlParseError> {
             }
 
             _ => {
-                current.push(chars.next().unwrap());
+                current.push(
+                    chars
+                        .next()
+                        .ok_or(SqlParseError::UnterminatedStringLiteral)?,
+                );
             }
         }
     }

+ 9 - 7
crates/cdk-sql-common/src/wallet/mod.rs

@@ -67,7 +67,6 @@ where
         Ok(())
     }
 
-    // FIXME: Replace unwrap with proper error handling
     async fn add_keyset_u32<T>(conn: &T) -> Result<(), Error>
     where
         T: DatabaseExecutor,
@@ -84,8 +83,9 @@ where
         .fetch_all(conn)
         .await?;
 
-        for id in keys_without_u32 {
-            let id = column_as_string!(id.first().unwrap());
+        for row in keys_without_u32 {
+            unpack_into!(let (id) = row);
+            let id = column_as_string!(id);
 
             if let Ok(id) = Id::from_str(&id) {
                 query(
@@ -115,8 +115,9 @@ where
         .fetch_all(conn)
         .await?;
 
-        for id in keysets_without_u32 {
-            let id = column_as_string!(id.first().unwrap());
+        for row in keysets_without_u32 {
+            unpack_into!(let (id) = row);
+            let id = column_as_string!(id);
 
             if let Ok(id) = Id::from_str(&id) {
                 query(
@@ -712,7 +713,6 @@ ON CONFLICT(id) DO UPDATE SET
         Ok(())
     }
 
-    // FIXME: Replace unwrap with proper error handling
     async fn update_proofs(
         &self,
         added: Vec<ProofInfo>,
@@ -765,7 +765,9 @@ ON CONFLICT(id) DO UPDATE SET
                 proof
                     .proof
                     .witness
-                    .map(|w| serde_json::to_string(&w).unwrap()),
+                    .and_then(|w| serde_json::to_string(&w)
+                        .inspect_err(|e| tracing::error!("Failed to serialize witness: {:?}", e))
+                        .ok()),
             )
             .bind(
                 "dleq_e",