louthy/language-ext · error · NotSupportedException

NotSupportedException

Error message

NotSupportedException

What it means

Temperature.ToString() switches on the internal UnitType (K/C/F) and throws NotSupportedException when the unit field holds an undefined value. In normal use UnitType is always one of the three enum members, so this is a defensive guard against a corrupted or default-initialized Temperature whose Type field was set outside the enum's valid range.

Solutions

  1. Only construct Temperature through the public FromCelcius/FromFahrenheit/FromKelvin factories.
  2. Check UnitType is K, C, or F before operations on reflectively created instances.
  3. Fix the code that assigns an out-of-range UnitType instead of handling it at ToString.
  4. Catch NotSupportedException around formatting if inputs come from untrusted deserialization.

Example fix

// before
var t = new Temperature((UnitType)99, 20); // ToString -> NotSupportedException
// after
var t = UnitType.C == unit ? Temperature.FromCelcius(20) : Temperature.FromKelvin(293.15);
Defensive patterns

Strategy: type-guard

Validate before calling

bool ok = Enum.IsDefined(typeof(UnitType), t.Type);

Type guard

static bool HasValidUnit(Temperature t) => t.Type is UnitType.K or UnitType.C or UnitType.F;

Try / catch

try { s = t.ToString(); }
catch (NotSupportedException ex) { /* invalid UnitType on instance */ }

Prevention

When it happens

Trigger: Calling ToString() on a Temperature whose UnitType was created from an invalid cast, e.g. new Temperature((UnitType)7, 100) via internal/reflection paths, or a default(Temperature)-like state produced through unsafe initialization.

Common situations: Deserialization tools that set fields directly without validation; reflection-based test harnesses; casting arbitrary ints to UnitType and calling the internal constructor.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of louthy/language-ext@2f0e362824 (2026-09-15). Data as JSON: /api/errors/4fca05ca1773d608. Report an issue: GitHub.

Appendix: source

Thrown at LanguageExt.Core/Units of Measure/Temperature.cs:74

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static double FtoC(double x) => (x - 32.0) * 5.0 / 9.0;

    public override int GetHashCode() =>
        Value.GetHashCode();

    public override bool Equals(object? obj) =>
        obj is Temperature t && Equals(t);

    public bool Equals(Temperature rhs) =>
        Value.Equals(rhs.Value);

    public override string ToString() =>
        Type switch
        {
            UnitType.K => $"{Value} K",
            UnitType.C => $"{Value} °C",
            UnitType.F => $"{Value} °F",
            _          => throw new NotSupportedException(Type.ToString())
        };

    public Temperature Kelvin =>
        Type switch
        {
            UnitType.K => this,
            UnitType.C => new Temperature(UnitType.K, CtoK(Value)),
            UnitType.F => new Temperature(UnitType.K, FtoK(Value)),
            _          => throw new NotSupportedException(Type.ToString())
        };

    public double KValue =>
        Type switch
        {
            UnitType.K => Value,
            UnitType.C => CtoK(Value),
            UnitType.F => FtoK(Value),
            _          => throw new NotSupportedException(Type.ToString())

View on GitHub (pinned to 2f0e362824)