Humanizr/Humanizer · error · ArgumentOutOfRangeException
Unknown byte-size unit system.
Error message
Unknown byte-size unit system.
What it means
Thrown by ByteSizeExtensions.HumanizeCompositeWithUnitSystem when the unitSystem argument is neither Legacy (which is handled earlier and delegates to HumanizeComposite) nor within the [DecimalSi, BinaryIec] range. The method explicitly rejects undefined enum values before doing any work.
Source
Thrown at src/Humanizer/Bytes/ByteSizeExtensions.cs:591
/// <paramref name="unitSystem"/> is not defined or <paramref name="precision"/> is less than one.
/// </exception>
/// <exception cref="ArgumentNullException"><paramref name="separator"/> is <see langword="null"/>.</exception>
public static string HumanizeCompositeWithUnitSystem(
this ByteSize input,
ByteSizeUnitSystem unitSystem,
int precision = 2,
IFormatProvider? formatProvider = null,
string separator = " ",
bool toWords = false)
{
if (unitSystem == ByteSizeUnitSystem.Legacy)
{
return input.HumanizeComposite(precision, formatProvider, separator, toWords);
}
if (unitSystem is < ByteSizeUnitSystem.DecimalSi or > ByteSizeUnitSystem.BinaryIec)
{
throw new ArgumentOutOfRangeException(nameof(unitSystem), unitSystem, "Unknown byte-size unit system.");
}
if (precision < 1)
{
throw new ArgumentOutOfRangeException(nameof(precision), precision, "Precision must be greater than zero.");
}
ArgumentNullException.ThrowIfNull(separator);
formatProvider ??= CultureInfo.CurrentCulture;
var culture = formatProvider as CultureInfo;
if (culture is not null)
{
formatProvider = LocaleNumberFormattingOverrides.GetFormattingNumberFormat(culture);
}
var formatter = Configurator.GetFormatter(culture);
var builtInFormatter = formatter as DefaultFormatter;
if (builtInFormatter is not null &&View on GitHub (pinned to ffc2b77c0f)
Solutions
- Constrain the value to a named member before calling; map unknowns to Legacy.
- Validate with Enum.IsDefined at the API boundary.
- Use ByteSizeUnitSystem.DecimalSi or BinaryIec explicitly when the system is known.
Example fix
// before
var text = size.HumanizeCompositeWithUnitSystem((ByteSizeUnitSystem)configValue);
// after
var system = Enum.IsDefined(typeof(ByteSizeUnitSystem), configValue)
? (ByteSizeUnitSystem)configValue
: ByteSizeUnitSystem.Legacy;
var text = size.HumanizeCompositeWithUnitSystem(system); Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(ByteSizeUnitSystem), system))
throw new ArgumentOutOfRangeException(nameof(system)); Type guard
static bool IsSupportedSystem(ByteSizeUnitSystem s) =>
s is ByteSizeUnitSystem.Legacy or ByteSizeUnitSystem.DecimalSi or ByteSizeUnitSystem.BinaryIec; Prevention
- Validate the enum at the API boundary
- Map unknown values to Legacy
- Avoid casting arbitrary integers to the enum
When it happens
Trigger: Calling HumanizeCompositeWithUnitSystem with an undefined ByteSizeUnitSystem value (e.g. (ByteSizeUnitSystem)42), or forwarding a config/JSON-derived integer that does not map to a named member.
Common situations: Deserializing the enum from a source with different naming, casting an arbitrary integer, or a binding default that produced an out-of-range value.
Related errors
- Unknown byte-size unit system.
- Unit token '{unit.Symbol}' does not belong to the selected b
- A byte-size format can contain only one distinct unit token.
- Value is not in the correct format
- EiB is outside the range supported by ByteSize.Bits.
AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13).
Data as JSON: /api/errors/e93171b7ceb78570.
Report an issue: GitHub.