zeroclaw-labs/zeroclaw · error · anyhow::Error

No xAI OAuth code provided

Error message

No xAI OAuth code provided

What it means

`parse_code_from_redirect` (xAI) rejects empty or whitespace-only input before any parsing. The function accepts a full callback URL, a bare query string, or a raw code — but the trimmed input must be non-empty. This is the fail-fast guard for manual code entry paths.

Source

Thrown at crates/zeroclaw-providers/src/auth/xai_oauth.rs:401

        .split_whitespace()
        .nth(1)
        .ok_or_else(|| anyhow::Error::msg("xAI callback request missing path"))?;
    let code = parse_code_from_redirect(path, Some(expected_state))?;

    let body = "<html><body><h2>ZeroClaw xAI login complete</h2><p>You can close this tab.</p></body></html>";
    let response = format!(
        "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
        body.len(),
        body
    );
    let _ = stream.write_all(response.as_bytes()).await;
    Ok(code)
}

pub fn parse_code_from_redirect(input: &str, expected_state: Option<&str>) -> Result<String> {
    let trimmed = input.trim();
    if trimmed.is_empty() {
        anyhow::bail!("No xAI OAuth code provided");
    }
    let query = trimmed.split_once('?').map_or(trimmed, |(_, query)| query);
    let params = parse_query_params(query);
    if let Some(err) = params.get("error") {
        let desc = params
            .get("error_description")
            .cloned()
            .unwrap_or_else(|| "xAI OAuth authorization failed".to_string());
        anyhow::bail!("{err}: {desc}");
    }
    if let Some(expected) = expected_state {
        let actual = params
            .get("state")
            .ok_or_else(|| anyhow::Error::msg("xAI OAuth callback missing state parameter"))?;
        if actual != expected {
            anyhow::bail!("xAI OAuth state mismatch");
        }
    }

View on GitHub (pinned to 88bb9c8533)

Solutions

  1. Trim the input and require non-empty before calling — fail fast with your own message
  2. Re-prompt when the user submits nothing
  3. Check the variable feeding the call is actually wired to the clipboard or prompt

Example fix

// before
let code = parse_code_from_redirect(&input, None)?; // "No xAI OAuth code provided" on empty input

// after
let input = input.trim();
if input.is_empty() {
    anyhow::bail!("paste the code copied from the browser");
}
let code = parse_code_from_redirect(input, None)?;
Defensive patterns

Strategy: validation

Validate before calling

let input = input.trim();
if input.is_empty() {
    anyhow::bail!("paste the code copied from the browser");
}
let code = parse_code_from_redirect(input, expected_state)?;

Try / catch

match parse_code_from_redirect(&raw, expected_state) {
    Ok(code) => code,
    Err(e) if e.to_string().contains("No xAI OAuth code provided") => reprompt_for_code().await,
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Passing an empty string or whitespace to `parse_code_from_redirect(input, expected_state)`; a user submits an empty prompt when the code is pasted manually; the feeding variable was never assigned.

Common situations: Manual paste flows where the user presses enter without pasting; a default-empty variable in the caller; clipboard read returning nothing.

Understand the failure class

Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.

Related errors


AI-assisted analysis of zeroclaw-labs/zeroclaw@88bb9c8533 (2026-08-23). Data as JSON: /api/errors/58181cfddbf6c8c0. Report an issue: GitHub.