bchavez/Bogus · error · ArgumentException

Unknown method {methodName} can't be found.

Error message

Unknown method {methodName} can't be found.

What it means

Thrown by Tokenizer.Parse when a handlebars token '{{method}}' in the input string does not match any registered MustashMethod name. Method names are uppercased and namespaced like 'RANDOMIZER.NUMBER'; only methods on datasets marked with RegisterMustasheMethodsAttribute are registered.

Source

Thrown at Source/Bogus/Tokenizer.cs:64

   }

   public static string Parse(string str, params object[] dataSets)
   {
      //Recursive base case. If there are no more {{ }} handle bars,
      //return.
      var start = str.IndexOf("{{", StringComparison.Ordinal);
      var end = str.IndexOf("}}", StringComparison.Ordinal);
      if( start == -1 && end == -1 )
      {
         return str;
      }

      //We have some handlebars to process. Get the method name and arguments.
      ParseMustashText(str, start, end, out var methodName, out var arguments);

      if( !MustashMethods.Contains(methodName) )
      {
         throw new ArgumentException($"Unknown method {methodName} can't be found.");
      }

      //At this point, we have a methodName like: RANDOMIZER.NUMBER
      //and if the dataset was registered with RegisterMustasheMethodsAttribute
      //we should be able to extract the dataset given it's methodName.
      var dataSet = FindDataSetWithMethod(dataSets, methodName);

      //Considering arguments, lets get the best method overload
      //that maps to a registered MustashMethod.
      var mm = FindMustashMethod(methodName, arguments);
      var providedArgumentList = ConvertStringArgumentsToObjects(arguments, mm);
      var optionalArgs = mm.OptionalArgs.Take(mm.Method.GetParameters().Length - providedArgumentList.Length);
      var argumentList = providedArgumentList.Concat(optionalArgs).ToArray();

      //make the actual invocation.
      var fakeVal = mm.Method.Invoke(dataSet, argumentList).ToString();

      var sb = new StringBuilder();

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Use only registered method names (e.g. {{name.firstname}}, {{randomizer.number}}) - check available MustashMethods.
  2. Register custom dataset methods via RegisterMustasheMethodsAttribute and Tokenizer.RegisterMustashMethods.
  3. Verify the exact uppercased method name with Tokenizer.MustashMethods lookup before templating.

Example fix

// before
var s = "{{name.fullname}}".Parse(f.Name, f.Random);
// after (use registered method)
var s = "{{name.fullname}}".Parse(f); // ensure NAME.FULLNAME is registered; else {{name.firstname}}
Defensive patterns

Strategy: validation

Validate before calling

static string SafeParse(string template, params object[] datasets) {
    foreach (var m in System.Text.RegularExpressions.Regex.Matches(template, @"\{\{(.*?)\}"))
        if (!Tokenizer.MustashMethods.Contains(m.ToString().Trim('{}').ToUpperInvariant()))
            throw new InvalidOperationException($"unknown mustash method: {m}");
    return Tokenizer.Parse(template, datasets);
}

Type guard

bool AllTokensRegistered(string template) =>
    System.Text.RegularExpressions.Regex.Matches(template, @"\{\{(.*?)\}")
        .Cast<System.Text.RegularExpressions.Match>()
        .All(m => Tokenizer.MustashMethods.Contains(m.Groups[1].Value.ToUpperInvariant()));

Try / catch

try { return Tokenizer.Parse(template, datasets); }
catch (ArgumentException ex) when (ex.Message.Contains("Unknown method")) {
    // strip or correct the unknown token and retry
}

Prevention

When it happens

Trigger: Calling 'Hello {{name.fullname}}'.ParseWith(faker) where 'NAME.FULLNAME' is not a registered method (correct is 'NAME.FULLNAME' only if registered; typo or unknown dataset method triggers it).

Common situations: Typos in template tokens, using a method that is not exposed on a registered dataset, or calling Tokenizer.Parse without registering custom mustash methods.

Related errors


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