{"record":{"id":"199545d0f0a58610","repo":"bchavez/Bogus","slug":"invalid-country-code","errorCode":null,"errorMessage":"Invalid country code","messagePattern":"Invalid country code","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/DataSets/Address.cs","lineNumber":160,"sourceCode":"\n   /// <summary>\n   /// Get a random ISO 3166-1 country code.\n   /// </summary>\n   /// <param name=\"format\">The format the country code should be in.</param>\n   /// <returns>A random country code.</returns>\n   public string CountryCode(Iso3166Format format = Iso3166Format.Alpha2)\n   {\n      if( format == Iso3166Format.Alpha2 )\n      {\n         return GetRandomArrayItem(\"country_code\");\n      }\n\n      if( format == Iso3166Format.Alpha3 )\n      {\n         return GetRandomArrayItem(\"country_code_alpha_3\");\n      }\n\n      throw new ArgumentException(\"Invalid country code\", nameof(format));\n   }\n\n   /// <summary>\n   /// Get a random state state.\n   /// </summary>\n   /// <returns>A random state.</returns>\n   public string State()\n   {\n      return GetRandomArrayItem(\"state\");\n   }\n\n   /// <summary>\n   /// Get a state abbreviation.\n   /// </summary>\n   /// <returns>An abbreviation for a random state.</returns>\n   public string StateAbbr()\n   {\n      return GetRandomArrayItem(\"state_abbr\");","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/DataSets/Address.cs#L142-L178","documentation":"Address.CountryCode(Iso3166Format) only handles Iso3166Format.Alpha2 and Iso3166Format.Alpha3 explicitly; any other value falls through to an ArgumentException. The enum only defines those two members, so with strongly-typed code this branch is effectively unreachable - it exists as a defensive guard against an enum value supplied via an invalid cast.","triggerScenarios":"Casting an arbitrary integer to Iso3166Format, e.g. address.CountryCode((Iso3166Format)99) or (Iso3166Format)5. This happens when the format comes from deserialized config/JSON as an int and is cast to the enum without bounds checking.","commonSituations":"Reading a numeric country-code-format value from configuration or an API payload and casting it directly to Iso3166Format. Values other than 2 (Alpha2) and 3 (Alpha3) trip the guard.","solutions":["Use the named enum values directly: Iso3166Format.Alpha2 or Iso3166Format.Alpha3.","If the value originates as an int, validate Enum.IsDefined(typeof(Iso3166Format), value) before calling CountryCode.","Default the parameter so callers rarely need to pass it: address.CountryCode() uses Alpha2."],"exampleFix":"// before\nvar cc = address.CountryCode((Iso3166Format)incomingInt);\n\n// after\nif (!Enum.IsDefined(typeof(Iso3166Format), incomingInt))\n    throw new ArgumentException(\"Unsupported country code format\");\nvar cc = address.CountryCode((Iso3166Format)incomingInt);","handlingStrategy":"validation","validationCode":"Iso3166Format fmt = Iso3166Format.Alpha2;\nif (!Enum.IsDefined(typeof(Iso3166Format), fmt))\n    throw new ArgumentOutOfRangeException(nameof(fmt));\nvar cc = address.CountryCode(fmt);","typeGuard":"static bool IsValid(Iso3166Format f) => f == Iso3166Format.Alpha2 || f == Iso3166Format.Alpha3;","tryCatchPattern":null,"preventionTips":["Prefer named enum values over casts from int.","If the value is deserialized as an int, run Enum.IsDefined before casting.","Default the parameter at the call site so most callers never pass it."],"tags":["enum","argument-validation","address","defensive-guard"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}