Hmbown/CodeWhale · error

The Codewhale service returned an invalid user code

Error message

The Codewhale service returned an invalid user code

What it means

Thrown by `validate_user_code` in crates/cli/src/cloud.rs:1116 when the user_code returned by the device-authorization endpoint does not match the expected format: exactly 14 characters with hyphens at positions 4 and 9 (format XXXX-XXXX-XXXX) and every other character drawn from the unambiguous alphabet ABCDEFGHJKLMNPQRSTUVWXYZ23456789 (no 0, 1, I, O). The CLI validates it before showing the code to the user or polling the token endpoint.

Solutions

  1. Fix the device-authorization endpoint to emit codes in the XXXX-XXXX-XXXX shape using only ABCDEFGHJKLMNPQRSTUVWXYZ23456789
  2. Check for accidental lowercasing/normalization of the code field and preserve its case
  3. Verify the code field in the raw JSON response is populated and not truncated

Example fix

// before
{ "user_code": "abcd-efgh-jklm" }
// after
{ "user_code": "ABCD-EFGH-JKLM" }
Defensive patterns

Strategy: validation

Validate before calling

const ALPHABET: &[u8] = b"ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
fn is_valid_user_code(code: &str) -> bool {
    let b = code.as_bytes();
    b.len() == 14
        && b[4] == b'-' && b[9] == b'-'
        && b.iter().enumerate()
            .all(|(i, c)| matches!(i, 4 | 9) || ALPHABET.contains(c))
}

Type guard

fn user_code_shape_ok(code: &str) -> bool {
    is_valid_user_code(code)
}

Try / catch

match run_device_login() {
    Err(e) if e.to_string().contains("invalid user code") => {
        eprintln!("Server user_code format mismatch; expected XXXX-XXXX-XXXX from ABCDEFGHJKLMNPQRSTUVWXYZ23456789.");
    }
    other => return other,
}

Prevention

When it happens

Trigger: The service returns a user code like `ABCD-EFGH-IJKL` (contains forbidden I/1/O/0), `ABCDEF-GHJK` (wrong hyphen positions), a 13- or 15-character string, a lowercase code `abcd-efgh-jklm`, or an empty/null code field.

Common situations: A mock/staging server generating codes with the full alphanumeric set or the standard base32; a legacy server using a different grouping (e.g. 4-4-4 with different hyphen positions); serialization that lowercases the code.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/955f2f9afdd44049. Report an issue: GitHub.

Appendix: source

Thrown at crates/cli/src/cloud.rs:1116

        .unwrap_or(host);
    host.eq_ignore_ascii_case("localhost")
        || host
            .parse::<IpAddr>()
            .is_ok_and(|address| address.is_loopback())
}

fn validate_user_code(code: &str) -> Result<()> {
    const ALPHABET: &[u8] = b"ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
    let bytes = code.as_bytes();
    if bytes.len() != 14
        || bytes[4] != b'-'
        || bytes[9] != b'-'
        || bytes
            .iter()
            .enumerate()
            .any(|(index, byte)| !matches!(index, 4 | 9) && !ALPHABET.contains(byte))
    {
        bail!("The Codewhale service returned an invalid user code");
    }
    Ok(())
}

fn validate_device_code(code: &str) -> Result<()> {
    if code.len() != 43
        || !code
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_'))
    {
        bail!("The Codewhale service returned an invalid device authorization response");
    }
    Ok(())
}

fn validate_api_key(key: &str) -> Result<()> {
    let bytes = key.len();
    if bytes < MIN_API_KEY_BYTES || bytes as u64 > MAX_API_KEY_BYTES {

View on GitHub (pinned to 73e0f67d83)