{"record":{"id":"3708060a08e30f88","repo":"Humanizr/Humanizer","slug":"unsupported-time-unit","errorCode":null,"errorMessage":"Unsupported time unit.","messagePattern":"Unsupported time unit\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Humanizer/Localisation/Formatters/DefaultFormatter.cs","lineNumber":149,"sourceCode":"    string IGrammaticalCaseTimeSpanFormatter.TimeSpanHumanize(\n        TimeUnit timeUnit,\n        int unit,\n        GrammaticalCase grammaticalCase)\n    {\n        if (GetType().Assembly != typeof(DefaultFormatter).Assembly)\n        {\n            throw new NotSupportedException(\n                $\"Custom formatter type '{GetType().FullName}' must explicitly implement {nameof(IGrammaticalCaseTimeSpanFormatter)} to support grammatical-case-aware durations.\");\n        }\n\n        if ((uint)grammaticalCase > (uint)GrammaticalCase.Causal)\n        {\n            throw new ArgumentOutOfRangeException(nameof(grammaticalCase), grammaticalCase, \"Unsupported grammatical case.\");\n        }\n\n        if ((uint)timeUnit > (uint)TimeUnit.Year)\n        {\n            throw new ArgumentOutOfRangeException(nameof(timeUnit), timeUnit, \"Unsupported time unit.\");\n        }\n\n        var table = LocaleDurationCaseTableCatalog.Resolve(Culture)\n            ?? throw new NotSupportedException(\n                $\"Culture '{Culture.Name}' has no grammatical-case classification for duration phrases.\");\n\n        if (table.Classification == LocaleDurationCaseClassification.Unsupported)\n        {\n            throw new NotSupportedException(\n                $\"Culture '{Culture.Name}' has an applicable grammatical case system, but verified duration forms are unavailable.\");\n        }\n\n        if (table.Classification == LocaleDurationCaseClassification.NotApplicable)\n        {\n            throw new NotSupportedException(\n                $\"Culture '{Culture.Name}' does not support grammatical-case duration phrases.\");\n        }\n","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/Humanizr/Humanizer/blob/ffc2b77c0f30d2fb176875841424379319d0ae9b/src/Humanizer/Localisation/Formatters/DefaultFormatter.cs#L131-L167","documentation":"Thrown in IGrammaticalCaseTimeSpanFormatter.TimeSpanHumanize when the timeUnit argument is not a defined member of the TimeUnit enum. The guard casts to uint and compares against (uint)TimeUnit.Year (the highest defined unit), so any value above Year or a negative-cast-to-large-uint fails. The exception is ArgumentOutOfRangeException named 'timeUnit' with the offending value.","triggerScenarios":"Calling the case-aware formatter with (TimeUnit)50; passing a TimeUnit deserialized from an out-of-range integer; default(TimeUnit) is the first member and is valid, so an uninitialized field will not throw (though it may produce the wrong unit).","commonSituations":"Storing TimeUnit as a raw int in config/database with a value outside the defined range; casting a foreign enum to TimeUnit; reflection-based dispatch that builds the unit dynamically.","solutions":["Validate with Enum.IsDefined(typeof(TimeUnit), timeUnit) before calling.","Bind configuration via strongly-typed enum parsing rather than raw int casts.","Use the standard TimeSpan.Humanize() entry points, which only ever pass defined units internally."],"exampleFix":"// before\nvar text = formatter.TimeSpanHumanize((TimeUnit)unitCode, count, grammaticalCase);\n\n// after\nif (!Enum.IsDefined(typeof(TimeUnit), unitCode))\n    throw new ArgumentOutOfRangeException(nameof(unitCode));\nvar text = formatter.TimeSpanHumanize((TimeUnit)unitCode, count, grammaticalCase);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(TimeUnit), timeUnit))\n    throw new ArgumentOutOfRangeException(nameof(timeUnit));\nvar text = caseFormatter.TimeSpanHumanize(timeUnit, unit, grammaticalCase);","typeGuard":"static bool IsValidTimeUnit(TimeUnit u) => Enum.IsDefined(typeof(TimeUnit), u);","tryCatchPattern":"try { return caseFormatter.TimeSpanHumanize(timeUnit, unit, grammaticalCase); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"timeUnit\")\n{ /* invalid unit; handle upstream */ throw; }","preventionTips":["Bind TimeUnit via enum parsing, not raw int casts.","Use the standard TimeSpan.Humanize() entry points which only pass defined units.","Validate deserialized TimeUnit values before formatting."],"tags":["timeunit","argument-validation","formatter","argument-out-of-range"],"backgroundTag":null,"analyzedSha":"ffc2b77c0f30d2fb176875841424379319d0ae9b","analyzedAt":"2026-08-13T21:42:34.584Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}