bchavez/Bogus · error · ArgumentException

One of the arguments for {mm.Name} cannot be converted to ta

Error message

One of the arguments for {mm.Name} cannot be converted to target type. Argument list: {string.Join(",", parameters)}

What it means

Filtered catch in Tokenizer.ConvertStringArgumentsToObjects (Tokenizer.cs:152) for InvalidCastException or FormatException from GetValueForParameter, rethrown as ArgumentException. The string could not be converted into the parameter's target type at all: wrong format or fundamentally incompatible value.

Source

Thrown at Source/Bogus/Tokenizer.cs:152

      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);

      return Convert.ChangeType(parameterValue, type);
   }

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Supply the argument in the exact culture-invariant format the target type expects (numbers use '.' as decimal, no thousands separators).
  2. For enum-typed params pass a valid defined member name or its integral value.
  3. If a value legitimately contains a comma, it cannot be expressed as a single template argument (the splitter splits on ','); redesign the template.

Example fix

// before
faker.Parse("{{random.number(\"abc\")}}");
// after
faker.Parse("{{random.number(42)}}");
Defensive patterns

Strategy: validation

Validate before calling

// Pre-convert each argument exactly as the tokenizer will, to surface format errors early.
static bool ConvertsTo(string value, Type targetType)
{
    var t = Nullable.GetUnderlyingType(targetType) ?? targetType;
    try
    {
        if (typeof(Enum).IsAssignableFrom(t)) return Enum.IsDefined(t, value) || int.TryParse(value, out _);
        if (typeof(TimeSpan) == t) return TimeSpan.TryParse(value, out _);
        Convert.ChangeType(value, t, CultureInfo.InvariantCulture);
        return true;
    }
    catch (InvalidCastException) { return false; }
    catch (FormatException) { return false; }
}

Try / catch

try { var s = faker.Parse(tpl); }
catch (ArgumentException ex) when (ex.InnerException is FormatException or InvalidCastException)
{
    // A template arg could not be converted to the target type.
    throw new InvalidOperationException($"Unconvertible arg in template: {tpl}", ex);
}

Prevention

When it happens

Trigger: Passing a non-numeric string to a numeric param, e.g. {{random.number("abc")}} makes Convert.ChangeType throw FormatException; passing a non-numeric to a numeric enum cast (InvalidCastException); passing a malformed value to a TimeSpan parameter via TimeSpan.Parse.

Common situations: Templating user-supplied or external strings into typed args without validation; locale-specific number formatting (decimal separators) since Convert.ChangeType for numbers is culture-invariant; commas inside a value splitting it into multiple args that then mismatch types.

Related errors


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