mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-24 13:42:15 +00:00
feat: Replace unwrap_or with proper error handling in credit_management
- Replace unwrap_or(0) with map_err for get operation in deduct_credit - Replace unwrap_or(0) with ? operator in check_credit - Add RedisOperation error mapping for current_balance retrieval
This commit is contained in:
@@ -38,7 +38,8 @@ impl crate::charging::ChargingEngine {
|
||||
let key = format!("credit:{}", ip);
|
||||
|
||||
// Check current balance first
|
||||
let current_balance: u64 = redis::AsyncCommands::get(&mut conn, &key).await.unwrap_or(0);
|
||||
let current_balance: u64 = redis::AsyncCommands::get(&mut conn, &key).await
|
||||
.map_err(|e| crate::errors::ChargingError::RedisOperation(e.to_string()))?;
|
||||
|
||||
if current_balance < bytes_used {
|
||||
return Err(ChargingError::InsufficientCredit {
|
||||
@@ -55,7 +56,7 @@ impl crate::charging::ChargingEngine {
|
||||
}
|
||||
|
||||
pub async fn check_credit(&self, ip: &str, bytes_requested: u64) -> ChargingResult<bool> {
|
||||
let current_balance = self.get_balance(ip).await.unwrap_or(0);
|
||||
let current_balance = self.get_balance(ip).await?;
|
||||
Ok(current_balance >= bytes_requested)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user