{"record":{"id":"2eb1d9ecc85dc8a7","repo":"tonhowtf/omniget","slug":"api-client-init-failed","errorCode":null,"errorMessage":"api client init failed: {}","messagePattern":"api client init failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src-tauri/omniget-core/src/platforms/bilibili/mod.rs","lineNumber":326,"sourceCode":"\n    Ok(DownloadResult {\n        file_path: result.final_path,\n        file_size_bytes: result.bytes,\n        duration_seconds: parsed\n            .items\n            .first()\n            .and_then(|i| i.duration_seconds)\n            .unwrap_or(0.0),\n        torrent_id: None,\n    })\n}\n\nfn build_api_client(\n    slug: Option<&str>,\n    user_agent: Option<&str>,\n) -> anyhow::Result<api::ApiClient> {\n    let mut client =\n        api::ApiClient::new().map_err(|e| anyhow!(\"api client init failed: {}\", e.i18n_key()))?;\n    if let Some(ua) = user_agent.filter(|s| !s.is_empty()) {\n        client = client.with_user_agent(ua);\n    }\n    match slug {\n        Some(s) => client = client.with_account(s),\n        None => client = client.with_anonymous_cookies(),\n    }\n    Ok(client)\n}\n\nfn sanitize(s: &str) -> String {\n    let cleaned = sanitize_filename::sanitize(s);\n    if cleaned.is_empty() {\n        \"video\".to_string()\n    } else {\n        cleaned\n    }\n}","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/omniget-core/src/platforms/bilibili/mod.rs#L308-L344","documentation":"build_api_client() constructs the bilibili api::ApiClient. If ApiClient::new() fails (HTTP client construction, e.g. TLS backend init or invalid default headers), the error is wrapped as 'api client init failed'. This happens before any network request is made, so it indicates a local client-configuration problem rather than a bilibili-side failure.","triggerScenarios":"api_engine_download() -> build_api_client() where api::ApiClient::new() returns Err — typically reqwest client construction failure (TLS backend unavailable, proxy env var malformed, system CA issues).","commonSituations":"Malformed HTTP_PROXY/HTTPS_PROXY environment variables; missing system root certificates or broken OpenSSL installation; an invalid user_agent string that the client rejects during builder setup.","solutions":["Check HTTP_PROXY/HTTPS_PROXY/ALL_PROXY env vars are valid URLs or unset them.","Verify TLS backend/native certificates are installed correctly (ca-certificates package up to date).","Confirm the provided user_agent string is sane (printable, no control characters).","Log the underlying error source (e.is_source chain) — anyhow's i18n_key may hide the real reqwest cause."],"exampleFix":"// before\nlet mut client = api::ApiClient::new().map_err(|e| anyhow!(\"api client init failed: {}\", e.i18n_key()))?;\n// after\nlet mut client = api::ApiClient::new().with_context(||\n    \"api client init failed (check proxy env vars and TLS certificates)\".to_string())?;","handlingStrategy":"try-catch","validationCode":"// validate environment before calling the download API\nfor v in [\"HTTP_PROXY\", \"HTTPS_PROXY\", \"ALL_PROXY\"] {\n    if let Ok(p) = std::env::var(v) { assert!(p.starts_with(\"http\"), \"{} must be a valid URL\", v); }\n}","typeGuard":null,"tryCatchPattern":"match download(url).await {\n    Err(e) if e.to_string().contains(\"api client init failed\") => {\n        eprintln!(\"check proxy env vars and TLS certificates\");\n        Err(e)\n    }\n    other => other,\n}","preventionTips":["Keep HTTP(S)_PROXY env vars valid or unset when not needed.","Install/refresh system ca-certificates so the TLS backend initializes.","Sanitize user_agent strings passed to the API client.","Log anyhow's full error chain to expose the underlying reqwest cause."],"tags":["bilibili","http-client","initialization","tls"],"backgroundTag":"module-init-failed","analyzedSha":"8600b91f4246848bac346874daa9e61c1fc5677a","analyzedAt":"2026-09-12T14:29:19.317Z","contentChangedAt":"2026-09-12T14:29:19.317Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}