egametang/ET · error · Exception

string mode < 0: {strText} {mode}

Error message

string mode < 0: {strText} {mode}

What it means

StringHashHelper.Mode(string, int) computes strText.GetLongHashCode() % mode to pick a bucket. It throws when mode is zero or negative because modulo by non-positive is undefined, so the bucket count must be at least one.

Source

Thrown at Packages/cn.etetet.core/Scripts/Core/Share/Helper/StringHashHelper.cs:29

            const uint seed = 1313; // 31 131 1313 13131 131313 etc..
            
            ulong hash = 0;
            for (int i = 0; i < str.Length; ++i)
            {
                char c = str[i];
                byte high = (byte)(c >> 8);
                byte low = (byte)(c & byte.MaxValue);
                hash = hash * seed + high;
                hash = hash * seed + low;
            }
            return (long)hash;
        }

        public static int Mode(this string strText, int mode)
        {
            if (mode <= 0)
            {
                throw new Exception($"string mode < 0: {strText} {mode}");
            }
            return (int)((ulong)strText.GetLongHashCode() % (uint)mode);
        }
    }
}

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Guard callers: only call Mode when the bucket count is >= 1.
  2. Fix the data source feeding mode (config, server count) so it is never 0.
  3. Prefer throwing an ArgumentException with the parameter name for a usage contract.

Example fix

// before
int bucket = key.Mode(servers.Count); // throws when Count==0
// after
if (servers.Count <= 0) throw new InvalidOperationException("no servers to route to");
int bucket = key.Mode(servers.Count);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(text)) throw new ArgumentException(nameof(text));
if (mode <= 0) throw new ArgumentOutOfRangeException(nameof(mode), "mode must be >= 1");
int bucket = text.Mode(mode);

Type guard

public static bool IsValidMode(int mode) => mode > 0;

Try / catch

null

Prevention

When it happens

Trigger: Calling text.Mode(count) where count is 0 (empty collection) or negative, e.g. sharding across servers when the server list is empty, or routing by a hash bucket sized from an uninitialized config value.

Common situations: Server/cluster list empty at startup, a config count field left at 0, or a list filtered down to zero elements before being used as the mode.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/f4edf2c961d87f9a. Report an issue: GitHub.