Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: replace unwrap in sanitize_legacy_fields with safe guard (#2893)

Louis Beaumont0fea81227127
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/store.rs b/apps/screenpipe-app-tauri/src-tauri/src/store.rs
index 2d8e321ab..b9cd11eee 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/store.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/store.rs
@@ -632,10 +632,12 @@ impl SettingsStore {
"unknown AI provider '{}' in preset, falling back to 'custom'",
provider
);
- preset.as_object_mut().unwrap().insert(
- "provider".to_string(),
- Value::String("custom".to_string()),
- );
+ if let Some(obj) = preset.as_object_mut() {
+ obj.insert(
+ "provider".to_string(),
+ Value::String("custom".to_string()),
+ );
+ }
}
}
}
@@ -1003,3 +1005,27 @@ impl PipeSuggestionsSettingsStore {
store.save().map_err(|e| e.to_string())
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use serde_json::json;
+
+ #[test]
+ fn test_sanitize_legacy_fields_does_not_panic() {
+ let mut corrupted = json!({
+ "aiPresets": ["corrupted_string_not_an_object"]
+ });
+
+ let sanitized = SettingsStore::sanitize_legacy_fields(corrupted);
+
+ // And let's test a valid object with missing/unknown provider to prove it works
+ let mut valid = json!({
+ "aiPresets": [{"provider": "unknown_provider"}]
+ });
+ let sanitized2 = SettingsStore::sanitize_legacy_fields(valid);
+
+ let presets = sanitized2.get("aiPresets").unwrap().as_array().unwrap();
+ assert_eq!(presets[0].get("provider").unwrap().as_str().unwrap(), "custom");
+ }
+}