clockworklabs/SpacetimeDB · error · ArgumentException

Expected ConnectionId hex string to be 32 characters long, b

Error message

Expected ConnectionId hex string to be 32 characters long, but was {hex.Length}.

What it means

ConnectionId.FromHexString requires exactly 32 hex characters because a ConnectionId is a 16-byte value and the hex parser does not strip prefixes, separators, or padding. Any other length is rejected with ArgumentException before any parsing happens.

Source

Thrown at crates/bindings-csharp/BSATN.Runtime/Builtins.cs:178

    /// Returns null if the resulting ConnectionId is the default.
    /// </summary>
    /// <param name="bytes"></param>
    public static ConnectionId? FromBigEndian(ReadOnlySpan<byte> bytes)
    {
        var id = Util.Read<ConnectionId>(bytes, littleEndian: false);
        return id == default ? null : id;
    }

    /// <summary>
    /// Create a ConnectionId from a hex string.
    /// </summary>
    /// <param name="hex"></param>
    /// <returns></returns>
    public static ConnectionId? FromHexString(string hex)
    {
        if (hex.Length != 32)
        {
            throw new ArgumentException(
                $"Expected ConnectionId hex string to be 32 characters long, but was {hex.Length}.",
                nameof(hex)
            );
        }

        return FromBigEndian(Util.StringToByteArray(hex));
    }

    public static ConnectionId Random()
    {
        var random = new Random();
        var id = new ConnectionId();
        random.NextBytes(Util.AsBytes(ref id));
        return id;
    }

    // --- auto-generated ---
    public readonly struct BSATN : IReadWrite<ConnectionId>

View on GitHub (pinned to 524b4487d9)

Solutions

  1. Pass exactly 32 hex characters (16 bytes) with no 0x prefix, dashes, braces, or whitespace
  2. Double-check you have the right ID type: ConnectionId = 32 chars, Identity = 64 chars
  3. Normalize input before calling: strip 0x/dashes/whitespace, then verify length == 32
  4. Source IDs from ConnectionId.ToHexString()/ ToString outputs, which are already canonical

Example fix

// before
var id = ConnectionId.FromHexString(raw.Trim()); // "0x6a1c..." -> 34 chars, throws

// after
var hex = raw.Trim().Replace("-", "");
if (hex.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) hex = hex[2..];
var id = ConnectionId.FromHexString(hex);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidConnectionIdHex(string hex) =>
    hex.Length == 32 && hex.All(c => c is (>= '0' and <= '9') or (>= 'a' and <= 'f') or (>= 'A' and <= 'F'));

if (IsValidConnectionIdHex(hex)) { var id = ConnectionId.FromHexString(hex); }

Prevention

When it happens

Trigger: Passing a string with a "0x" prefix (34 chars); a dash-formatted UUID-like string (36 chars); an Identity hex string (64 chars); an empty string; a hex string that was truncated or had whitespace appended.

Common situations: Copy-pasting IDs of the wrong kind (Identity vs ConnectionId vs Address) from logs or dashboards; concatenating/parsing IDs with formatting helpers that add prefixes or newlines.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@524b4487d9 (2026-08-16). Data as JSON: /api/errors/27d241022ce49791. Report an issue: GitHub.