Skip to content

Commit 5a13d99

Browse files
committed
fix: Handle serialization errors properly in batch UPDATE
- Replace .unwrap() with proper error handling for serde_json::to_vec() - Fail early if any row serialization fails before applying batch - Maintain atomicity guarantee by preventing partial updates on serialization errors - Add descriptive error message for serialization failures Addresses CodeRabbit feedback on potential panic in batch operation.
1 parent cbd62fc commit 5a13d99

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

nexum_core/src/executor/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,17 @@ impl Executor {
240240
// Phase 2: Apply all updates atomically using batch operation
241241
let updated_count = updates.len();
242242
if !updates.is_empty() {
243-
let batch_operations: Vec<(Vec<u8>, Vec<u8>)> = updates
244-
.into_iter()
245-
.map(|(key, row)| {
246-
let value = serde_json::to_vec(&row).unwrap();
247-
(key, value)
248-
})
249-
.collect();
243+
let mut batch_operations: Vec<(Vec<u8>, Vec<u8>)> = Vec::new();
244+
245+
// Serialize all rows first, fail early if any serialization fails
246+
for (key, row) in updates {
247+
let value = serde_json::to_vec(&row).map_err(|e| {
248+
StorageError::WriteError(format!("Failed to serialize row: {}", e))
249+
})?;
250+
batch_operations.push((key, value));
251+
}
252+
253+
// Only apply batch if all serializations succeeded
250254
self.storage.batch_set(batch_operations)?;
251255
}
252256

0 commit comments

Comments
 (0)