{"record":{"id":"fcc552348b4f9779","repo":"tonhowtf/omniget","slug":"e-to-string-fcc552","errorCode":null,"errorMessage":"e.to_string()","messagePattern":"e\\.to_string\\(\\)","errorType":"exception","errorClass":"std::io::Error","httpStatus":null,"severity":"error","filePath":"src-tauri/src/platforms/bilibili/cookie.rs","lineNumber":73,"sourceCode":"        Some(p) => p,\n        None => return AnonymousCookieState::default(),\n    };\n    let content = match std::fs::read_to_string(&path) {\n        Ok(c) => c,\n        Err(_) => return AnonymousCookieState::default(),\n    };\n    serde_json::from_str(&content).unwrap_or_default()\n}\n\npub fn save_state(state: &AnonymousCookieState) -> std::io::Result<()> {\n    let path = meta_file_path().ok_or_else(|| {\n        std::io::Error::new(std::io::ErrorKind::Other, \"app_data_dir unavailable\")\n    })?;\n    if let Some(parent) = path.parent() {\n        std::fs::create_dir_all(parent)?;\n    }\n    let serialized = serde_json::to_string_pretty(state)\n        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;\n    std::fs::write(path, serialized)\n}\n\npub async fn ensure_fresh() -> Result<AnonymousCookieState> {\n    let cached = load_state();\n    if cached.is_fresh() {\n        write_netscape_for_anonymous(&cached).ok();\n        return Ok(cached);\n    }\n    bootstrap_anonymous().await\n}\n\npub async fn bootstrap_anonymous() -> Result<AnonymousCookieState> {\n    let ua = super::api::DEFAULT_USER_AGENT;\n    let now_ms = now_ms();\n    let uuid = gen_uuid_b_lsid();\n    let b_lsid = gen_b_lsid();\n    let b_nut = now_ms.to_string();","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/src/platforms/bilibili/cookie.rs#L55-L91","documentation":"Inside save_state, serde_json::to_string_pretty serializes AnonymousCookieState; if serialization fails the anyhow/std error is converted into an io::Error with ErrorKind::InvalidData and message e.to_string() (the serde error text, e.g. \"failed to serialize...\" — AnonymousCookieState derives Serialize so this is rare). The message is dynamic, hence the placeholder name.","triggerScenarios":"bootstrap_anonymous → save_state with an AnonymousCookieState that serde cannot serialize — practically only possible if a non-serializable value (e.g. a map with non-string keys or a poisoned custom Serialize impl) is present, or if the struct gains a field with a failing custom serializer.","commonSituations":"After a code change adds a field whose Serialize impl errors; misuse of serde_json::Value containing invalid data injected into the state; version mismatch between omniget-core crates sharing the struct.","solutions":["Inspect the inner serde message (the error text) to find which field failed serialization.","Audit AnonymousCookieState fields for custom Serialize impls or non-string map keys and fix them.","Keep the struct simple (String/u64/i64 fields) so to_string_pretty cannot fail.","Consider using a typed error (serde_json::Error) instead of flattening into io::Error for better diagnostics."],"exampleFix":"// before\nlet serialized = serde_json::to_string_pretty(state)\n    .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;\n\n// after\nlet serialized = serde_json::to_string_pretty(state)\n    .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, format!(\"serialize AnonymousCookieState: {e}\")))?;","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match save_state(&state) {\n    Err(e) if e.kind() == std::io::ErrorKind::InvalidData => {\n        tracing::error!(\"anonymous state serialization failed: {e}\");\n    }\n    r => r?,\n}","preventionTips":["Keep AnonymousCookieState fields to plain serializable primitives","Add a unit test that round-trips serde_json::to_string_pretty(load_state())","Include context in the mapped error message instead of bare e.to_string()"],"tags":["serialization","serde","rust","json"],"backgroundTag":"json-marshal-failed","analyzedSha":"8600b91f4246848bac346874daa9e61c1fc5677a","analyzedAt":"2026-09-12T14:29:19.317Z","contentChangedAt":"2026-09-12T14:29:19.317Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}