{"record":{"id":"ae3b5ee6962a01d9","repo":"bchavez/Bogus","slug":"one-of-the-arguments-for-mm-name-cannot-be-conve","errorCode":null,"errorMessage":"One of the arguments for {mm.Name} cannot be converted to target type. Argument list: {string.Join(\",\", parameters)}","messagePattern":"One of the arguments for (.+?) cannot be converted to target type\\. Argument list: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Tokenizer.cs","lineNumber":152,"sourceCode":"      var found = selection.FirstOrDefault();\n      return found ?? throw new ArgumentException($\"Cannot find a method '{methodName}' that could accept {arguments.Length} arguments\");\n   }\n\n   private static object[] ConvertStringArgumentsToObjects(string[] parameters, MustashMethod mm)\n   {\n      try\n      {\n         return mm.Method.GetParameters()\n                         .Zip(parameters, GetValueForParameter)\n                         .ToArray();\n      }\n      catch (OverflowException ex)\n      {\n         throw new ArgumentOutOfRangeException($\"One of the arguments for {mm.Name} is out of supported range. Argument list: {string.Join(\",\", parameters)}\", ex);\n      }\n      catch (Exception ex) when (ex is InvalidCastException or FormatException)\n      {\n         throw new ArgumentException($\"One of the arguments for {mm.Name} cannot be converted to target type. Argument list: {string.Join(\",\", parameters)}\", ex);\n      }\n      catch (Exception ex)\n      {\n         throw new ArgumentException($\"Cannot parse arguments for {mm.Name}. Argument list: {string.Join(\",\", parameters)}\", ex);\n      }\n   }\n\n   private static object GetValueForParameter(ParameterInfo parameterInfo, string parameterValue)\n   {\n      var type = Nullable.GetUnderlyingType(parameterInfo.ParameterType) ?? parameterInfo.ParameterType;\n\n      if( typeof(Enum).IsAssignableFrom(type)) return Enum.Parse(type, parameterValue);\n\n      if( typeof(TimeSpan) == type ) return TimeSpan.Parse(parameterValue);\n\n      return Convert.ChangeType(parameterValue, type);\n   }\n","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Tokenizer.cs#L134-L170","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Supply the argument in the exact culture-invariant format the target type expects (numbers use '.' as decimal, no thousands separators).","For enum-typed params pass a valid defined member name or its integral value.","If a value legitimately contains a comma, it cannot be expressed as a single template argument (the splitter splits on ','); redesign the template."],"exampleFix":"// before\nfaker.Parse(\"{{random.number(\\\"abc\\\")}}\");\n// after\nfaker.Parse(\"{{random.number(42)}}\");","handlingStrategy":"validation","validationCode":"// Pre-convert each argument exactly as the tokenizer will, to surface format errors early.\nstatic bool ConvertsTo(string value, Type targetType)\n{\n    var t = Nullable.GetUnderlyingType(targetType) ?? targetType;\n    try\n    {\n        if (typeof(Enum).IsAssignableFrom(t)) return Enum.IsDefined(t, value) || int.TryParse(value, out _);\n        if (typeof(TimeSpan) == t) return TimeSpan.TryParse(value, out _);\n        Convert.ChangeType(value, t, CultureInfo.InvariantCulture);\n        return true;\n    }\n    catch (InvalidCastException) { return false; }\n    catch (FormatException) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try { var s = faker.Parse(tpl); }\ncatch (ArgumentException ex) when (ex.InnerException is FormatException or InvalidCastException)\n{\n    // A template arg could not be converted to the target type.\n    throw new InvalidOperationException($\"Unconvertible arg in template: {tpl}\", ex);\n}","preventionTips":["Use culture-invariant numeric formatting ('.' decimal, no thousands separators) in templates.","Never embed commas inside a single argument value; the splitter cannot represent it.","Validate external/user strings before placing them into typed template arguments."],"tags":["mustache-template","type-conversion","tokenizer"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}