tonhowtf/omniget · error

api client init failed

Error message

api client init failed: {}

What it means

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.

Solutions

  1. Check HTTP_PROXY/HTTPS_PROXY/ALL_PROXY env vars are valid URLs or unset them.
  2. Verify TLS backend/native certificates are installed correctly (ca-certificates package up to date).
  3. Confirm the provided user_agent string is sane (printable, no control characters).
  4. Log the underlying error source (e.is_source chain) — anyhow's i18n_key may hide the real reqwest cause.

Example fix

// before
let mut client = api::ApiClient::new().map_err(|e| anyhow!("api client init failed: {}", e.i18n_key()))?;
// after
let mut client = api::ApiClient::new().with_context(||
    "api client init failed (check proxy env vars and TLS certificates)".to_string())?;
Defensive patterns

Strategy: try-catch

Validate before calling

// validate environment before calling the download API
for v in ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY"] {
    if let Ok(p) = std::env::var(v) { assert!(p.starts_with("http"), "{} must be a valid URL", v); }
}

Try / catch

match download(url).await {
    Err(e) if e.to_string().contains("api client init failed") => {
        eprintln!("check proxy env vars and TLS certificates");
        Err(e)
    }
    other => other,
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of tonhowtf/omniget@8600b91f42 (2026-09-12). Data as JSON: /api/errors/2eb1d9ecc85dc8a7. Report an issue: GitHub.

Appendix: source

Thrown at src-tauri/omniget-core/src/platforms/bilibili/mod.rs:326

    Ok(DownloadResult {
        file_path: result.final_path,
        file_size_bytes: result.bytes,
        duration_seconds: parsed
            .items
            .first()
            .and_then(|i| i.duration_seconds)
            .unwrap_or(0.0),
        torrent_id: None,
    })
}

fn build_api_client(
    slug: Option<&str>,
    user_agent: Option<&str>,
) -> anyhow::Result<api::ApiClient> {
    let mut client =
        api::ApiClient::new().map_err(|e| anyhow!("api client init failed: {}", e.i18n_key()))?;
    if let Some(ua) = user_agent.filter(|s| !s.is_empty()) {
        client = client.with_user_agent(ua);
    }
    match slug {
        Some(s) => client = client.with_account(s),
        None => client = client.with_anonymous_cookies(),
    }
    Ok(client)
}

fn sanitize(s: &str) -> String {
    let cleaned = sanitize_filename::sanitize(s);
    if cleaned.is_empty() {
        "video".to_string()
    } else {
        cleaned
    }
}

View on GitHub (pinned to 8600b91f42)