bchavez/Bogus · error · ArgumentOutOfRangeException

One of the arguments for

Error message

One of the arguments for {mm.Name} is out of supported range. Argument list: {string.Join(",", parameters)}

What it means

Wrapping handler in Tokenizer.ConvertStringArgumentsToObjects (Tokenizer.cs:148). While turning template string args into typed values (GetValueForParameter uses Convert.ChangeType / Enum.Parse / TimeSpan.Parse), an OverflowException was raised: the value parsed syntactically but exceeded the target type's representable range. Bogus rethrows it as ArgumentOutOfRangeException, preserving the original as InnerException.

Solutions

  1. Clamp the offending argument to the target type's range (int: +/-2,147,483,647; long: +/-9.2e18).
  2. If you need larger magnitudes, use a dataset method whose parameter accepts a wider type, if one exists.
  3. Read the error's 'Argument list:' to find which positional argument overflowed, then fix that one value.

Example fix

// before
faker.Parse("{{random.number(99999999999)}}");
// after
faker.Parse("{{random.number(1,1000000)}}");
Defensive patterns

Strategy: validation

Validate before calling

// For numeric arguments, verify they fit the target parameter type before parsing.
static bool Fits(string value, Type targetType)
{
    var t = Nullable.GetUnderlyingType(targetType) ?? targetType;
    try { Convert.ChangeType(value, t, CultureInfo.InvariantCulture); return true; }
    catch (OverflowException) { return false; }
    catch { return true; } // not a range problem; let the real converter handle it
}

Try / catch

try { var s = faker.Parse(tpl); }
catch (ArgumentOutOfRangeException ex) when (ex.Message.Contains("out of supported range"))
{
    // Inspect ex.Message 'Argument list:' to find the overflowing positional arg.
    throw new InvalidOperationException($"Numeric arg out of range in template: {tpl}", ex);
}

Prevention

When it happens

Trigger: A numeric argument whose magnitude exceeds the parameter type, e.g. {{random.number(99999999999)}} where Number's params are int (max 2,147,483,647), or a year/epoch value beyond a target numeric type's range, or an integral value outside an enum's underlying-type range.

Common situations: Hard-coding large IDs/timestamps/epochs in templates; forgetting that Randomizer.Number(min,max) takes int not long; locale/format differences producing unexpectedly large parsed numbers; passing a long-style value where an int param is expected.

Related errors


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

Appendix: source

Thrown at Source/Bogus/Tokenizer.cs:148

         where mm.Method.GetParameters().Length >= arguments.Length
         where mm.OptionalArgs.Length + arguments.Length >= mm.Method.GetParameters().Length
         select mm;

      var found = selection.FirstOrDefault();
      return found ?? throw new ArgumentException($"Cannot find a method '{methodName}' that could accept {arguments.Length} arguments");
   }

   private static object[] ConvertStringArgumentsToObjects(string[] parameters, MustashMethod mm)
   {
      try
      {
         return mm.Method.GetParameters()
                         .Zip(parameters, GetValueForParameter)
                         .ToArray();
      }
      catch (OverflowException ex)
      {
         throw new ArgumentOutOfRangeException($"One of the arguments for {mm.Name} is out of supported range. Argument list: {string.Join(",", parameters)}", ex);
      }
      catch (Exception ex) when (ex is InvalidCastException or FormatException)
      {
         throw new ArgumentException($"One of the arguments for {mm.Name} cannot be converted to target type. Argument list: {string.Join(",", parameters)}", ex);
      }
      catch (Exception ex)
      {
         throw new ArgumentException($"Cannot parse arguments for {mm.Name}. Argument list: {string.Join(",", parameters)}", ex);
      }
   }

   private static object GetValueForParameter(ParameterInfo parameterInfo, string parameterValue)
   {
      var type = Nullable.GetUnderlyingType(parameterInfo.ParameterType) ?? parameterInfo.ParameterType;

      if( typeof(Enum).IsAssignableFrom(type)) return Enum.Parse(type, parameterValue);

      if( typeof(TimeSpan) == type ) return TimeSpan.Parse(parameterValue);

View on GitHub (pinned to 6ece18c5c2)