{"record":{"id":"92c0fd87079ec7e6","repo":"bchavez/Bogus","slug":"nameof-type-is-not-valid","errorCode":null,"errorMessage":"{nameof(type)} is not valid.","messagePattern":"(.+?) is not valid\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/Vendor/UserAgentGenerator.cs","lineNumber":51,"sourceCode":"         return $\"10{delim}{this.Random.Number(5, 10)}{delim}{this.Random.Number(0, 9)}\";\n      }\n      if( type == \"chrome\" )\n      {\n         return $\"{this.Random.Number(13, 39)}.0.{this.Random.Number(800, 899)}.0\";\n      }\n      if( type == \"presto\" )\n      {\n         return $\"2.9.{this.Random.Number(160, 190)}\";\n      }\n      if( type == \"presto2\" )\n      {\n         return $\"{this.Random.Number(10, 12)}.00\";\n      }\n      if( type == \"safari\" )\n      {\n         return $\"{this.Random.Number(531, 538)}.{this.Random.Number(0, 2)}.{this.Random.Number(0, 2)}\";\n      }\n      throw new ArgumentOutOfRangeException($\"{nameof(type)} is not valid.\");\n   }\n\n   internal string RandomRevision(int dots)\n   {\n      var ver = string.Empty;\n      for( int i = 0; i < dots; i++ )\n      {\n         ver += \".\" + this.Random.Number(0, 9);\n      }\n      return ver;\n   }\n\n   private string RandomLanguage()\n   {\n      var languages = new[]\n         {\n            \"AB\", \"AF\", \"AN\", \"AR\", \"AS\", \"AZ\", \"BE\", \"BG\", \"BN\", \"BO\", \"BR\", \"BS\", \"CA\", \"CE\", \"CO\", \"CS\",\n            \"CU\", \"CY\", \"DA\", \"DE\", \"EL\", \"EN\", \"EO\", \"ES\", \"ET\", \"EU\", \"FA\", \"FI\", \"FJ\", \"FO\", \"FR\", \"FY\",","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/Vendor/UserAgentGenerator.cs#L33-L69","documentation":"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.","triggerScenarios":"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.","commonSituations":"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().","solutions":["Pass one of the eight valid tokens: net, nt, trident, osx, chrome, presto, presto2, safari.","If adding a new browser/version scheme, extend the if-chain before the throw (in a fork) rather than relying on any default.","Do not call this internal API from application code; use the public Internet.UserAgent() generator, which never produces an invalid type."],"exampleFix":"// before (internal, via reflection/fork)\nuaGen.VersionString(\"firefox\");\n// after\nuaGen.VersionString(\"safari\"); // a valid version-scheme token","handlingStrategy":"validation","validationCode":"// Whitelist the only valid version-scheme tokens before calling the internal generator.\nstatic readonly HashSet<string> ValidVersionTypes = new(StringComparer.Ordinal)\n{ \"net\", \"nt\", \"trident\", \"osx\", \"chrome\", \"presto\", \"presto2\", \"safari\" };\nstatic string SafeVersionString(object uaGen, string type)\n{\n    if (!ValidVersionTypes.Contains(type))\n        throw new ArgumentOutOfRangeException(nameof(type), $\"'{type}' is not a valid version-scheme token.\");\n    // call via reflection / internals only if you truly must\n    return (string)uaGen.GetType().GetMethod(\"VersionString\", BindingFlags.Instance | BindingFlags.NonPublic)\n                          .Invoke(uaGen, new object[] { type, \".\" });\n}","typeGuard":"static bool IsValidVersionType(string type) =>\n    type is \"net\" or \"nt\" or \"trident\" or \"osx\" or \"chrome\" or \"presto\" or \"presto2\" or \"safari\";","tryCatchPattern":"// Prefer not to call this internal API at all; if you do:\ntry { ver = uaGen.VersionString(type); }\ncatch (ArgumentOutOfRangeException ex) when (ex.Message.Contains(\"type is not valid\"))\n{\n    throw new InvalidOperationException($\"'{type}' is not a valid UserAgent version-scheme token.\", ex);\n}","preventionTips":["Use the public Internet.UserAgent() API; it never produces an invalid type.","Remember the valid tokens are version-scheme selectors, not browser names (firefox/edge are invalid here).","If you fork to add a browser, add its version case to VersionString before the throw, not after."],"tags":["user-agent","argument-range","vendor","internal"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}