feat: Add resilience hardening for csv parser

This commit is contained in:
2026-07-03 08:04:14 +05:30
parent a305bba66d
commit be6c95c497
3 changed files with 69 additions and 3 deletions

View File

@@ -75,14 +75,24 @@ def parse_and_validate_csv(content: bytes) -> List[Dict[str, Any]]:
else None
)
if not all([txn_id, date_str, amount_str, merchant]):
if not all([date_str, amount_str, merchant]):
raise InvalidCSVException(
f"Row {line_num}: Empty values are not allowed in required fields"
f"Row {line_num}: Empty values are not allowed in required fields (date, amount, merchant)"
)
# Auto-generate txn_id if missing or empty
if not txn_id or not txn_id.strip():
import uuid
txn_id = f"GEN_{uuid.uuid4().hex[:8].upper()}"
else:
txn_id = txn_id.strip()
# Validate amount
cleaned_amount_str = (
amount_str.strip().replace("$", "").replace("", "").replace(",", "")
)
try:
amount = float(amount_str.strip())
amount = float(cleaned_amount_str)
except ValueError as e:
raise InvalidCSVException(
f"Row {line_num}: Invalid amount '{amount_str}'. Must be a number."