Browse Source

Add a custom error for invalid amounts

Cesar Rodas 1 month ago
parent
commit
f1c69fe5da
1 changed files with 6 additions and 1 deletions
  1. 6 1
      crates/cashu/src/amount.rs

+ 6 - 1
crates/cashu/src/amount.rs

@@ -23,6 +23,9 @@ pub enum Error {
     /// Cannot convert units
     #[error("Cannot convert units")]
     CannotConvertUnits,
+    /// Invalid amount
+    #[error("Invalid Amount: {0}")]
+    InvalidAmount(String),
 }
 
 /// Amount can be any unit
@@ -35,7 +38,9 @@ impl FromStr for Amount {
     type Err = Error;
 
     fn from_str(s: &str) -> Result<Self, Self::Err> {
-        let value = s.parse::<u64>().map_err(|_| Error::AmountOverflow)?;
+        let value = s
+            .parse::<u64>()
+            .map_err(|_| Error::InvalidAmount(s.to_owned()))?;
         Ok(Amount(value))
     }
 }