bchavez/Bogus · error · ArgumentOutOfRangeException

{nameof(type)} is not valid.

Error message

{nameof(type)} is not valid.

What it means

Thrown by UserAgentGenerator.VersionString (UserAgentGenerator.cs:51) when 'type' is none of the eight recognized version-scheme selectors: net, nt, trident, osx, chrome, presto, presto2, safari. The method is internal, so through the normal Internet.UserAgent()/Generate() flow it is only ever called with those hardcoded literals (see BrowserAgent) and is effectively unreachable for external callers. It surfaces only via reflection, an InternalsVisibleTo friend, or a fork/extension of the generator.

Source

Thrown at Source/Bogus/Vendor/UserAgentGenerator.cs:51

         return $"10{delim}{this.Random.Number(5, 10)}{delim}{this.Random.Number(0, 9)}";
      }
      if( type == "chrome" )
      {
         return $"{this.Random.Number(13, 39)}.0.{this.Random.Number(800, 899)}.0";
      }
      if( type == "presto" )
      {
         return $"2.9.{this.Random.Number(160, 190)}";
      }
      if( type == "presto2" )
      {
         return $"{this.Random.Number(10, 12)}.00";
      }
      if( type == "safari" )
      {
         return $"{this.Random.Number(531, 538)}.{this.Random.Number(0, 2)}.{this.Random.Number(0, 2)}";
      }
      throw new ArgumentOutOfRangeException($"{nameof(type)} is not valid.");
   }

   internal string RandomRevision(int dots)
   {
      var ver = string.Empty;
      for( int i = 0; i < dots; i++ )
      {
         ver += "." + this.Random.Number(0, 9);
      }
      return ver;
   }

   private string RandomLanguage()
   {
      var languages = new[]
         {
            "AB", "AF", "AN", "AR", "AS", "AZ", "BE", "BG", "BN", "BO", "BR", "BS", "CA", "CE", "CO", "CS",
            "CU", "CY", "DA", "DE", "EL", "EN", "EO", "ES", "ET", "EU", "FA", "FI", "FJ", "FO", "FR", "FY",

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Pass one of the eight valid tokens: net, nt, trident, osx, chrome, presto, presto2, safari.
  2. If adding a new browser/version scheme, extend the if-chain before the throw (in a fork) rather than relying on any default.
  3. Do not call this internal API from application code; use the public Internet.UserAgent() generator, which never produces an invalid type.

Example fix

// before (internal, via reflection/fork)
uaGen.VersionString("firefox");
// after
uaGen.VersionString("safari"); // a valid version-scheme token
Defensive patterns

Strategy: validation

Validate before calling

// Whitelist the only valid version-scheme tokens before calling the internal generator.
static readonly HashSet<string> ValidVersionTypes = new(StringComparer.Ordinal)
{ "net", "nt", "trident", "osx", "chrome", "presto", "presto2", "safari" };
static string SafeVersionString(object uaGen, string type)
{
    if (!ValidVersionTypes.Contains(type))
        throw new ArgumentOutOfRangeException(nameof(type), $"'{type}' is not a valid version-scheme token.");
    // call via reflection / internals only if you truly must
    return (string)uaGen.GetType().GetMethod("VersionString", BindingFlags.Instance | BindingFlags.NonPublic)
                          .Invoke(uaGen, new object[] { type, "." });
}

Type guard

static bool IsValidVersionType(string type) =>
    type is "net" or "nt" or "trident" or "osx" or "chrome" or "presto" or "presto2" or "safari";

Try / catch

// Prefer not to call this internal API at all; if you do:
try { ver = uaGen.VersionString(type); }
catch (ArgumentOutOfRangeException ex) when (ex.Message.Contains("type is not valid"))
{
    throw new InvalidOperationException($"'{type}' is not a valid UserAgent version-scheme token.", ex);
}

Prevention

When it happens

Trigger: Calling VersionString (via reflection/internals/fork) with an unrecognized type, e.g. "firefox" or "edge". Note these are BROWSER names, not valid here: Firefox/IE versions are computed inline in BrowserAgent, and the valid tokens are version-scheme selectors, not browser names.

Common situations: Extending the vendor generator to add a browser and forgetting to add the matching case before the throw; passing a browser name where a version-scheme token is expected; reaching into the internal API from application code instead of the public Internet.UserAgent().

Related errors


AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13). Data as JSON: /api/errors/92c0fd87079ec7e6. Report an issue: GitHub.