bchavez/Bogus · error · ArgumentException
Invalid country code
Error message
Invalid country code
What it means
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.
Source
Thrown at Source/Bogus/DataSets/Address.cs:160
/// <summary>
/// Get a random ISO 3166-1 country code.
/// </summary>
/// <param name="format">The format the country code should be in.</param>
/// <returns>A random country code.</returns>
public string CountryCode(Iso3166Format format = Iso3166Format.Alpha2)
{
if( format == Iso3166Format.Alpha2 )
{
return GetRandomArrayItem("country_code");
}
if( format == Iso3166Format.Alpha3 )
{
return GetRandomArrayItem("country_code_alpha_3");
}
throw new ArgumentException("Invalid country code", nameof(format));
}
/// <summary>
/// Get a random state state.
/// </summary>
/// <returns>A random state.</returns>
public string State()
{
return GetRandomArrayItem("state");
}
/// <summary>
/// Get a state abbreviation.
/// </summary>
/// <returns>An abbreviation for a random state.</returns>
public string StateAbbr()
{
return GetRandomArrayItem("state_abbr");View on GitHub (pinned to 6ece18c5c2)
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.
Example fix
// before
var cc = address.CountryCode((Iso3166Format)incomingInt);
// after
if (!Enum.IsDefined(typeof(Iso3166Format), incomingInt))
throw new ArgumentException("Unsupported country code format");
var cc = address.CountryCode((Iso3166Format)incomingInt); Defensive patterns
Strategy: validation
Validate before calling
Iso3166Format fmt = Iso3166Format.Alpha2;
if (!Enum.IsDefined(typeof(Iso3166Format), fmt))
throw new ArgumentOutOfRangeException(nameof(fmt));
var cc = address.CountryCode(fmt); Type guard
static bool IsValid(Iso3166Format f) => f == Iso3166Format.Alpha2 || f == Iso3166Format.Alpha3;
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Gender not handled.
- The locale '{locale}' does not exist. To see all available l
- The country code must be an ISO3166 two-letter country code.
- The specified property '{property.Name}' does not have a set
- [{registrationNumberType}] is not a supported [{nameof(VatRe
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/199545d0f0a58610.
Report an issue: GitHub.