louthy/language-ext · error · NotSupportedException

NotSupportedException: Type.ToString()

Error message

NotSupportedException: Type.ToString()

What it means

Same NotSupportedException guard in Temperature.Add(Temperature), in the UnitType.C arm: with the left side in Celsius, the right side's unit is not K, C, or F, so the Celsius-based addition has no conversion rule and the library throws NotSupportedException whose message is Type.ToString().

Solutions

  1. Re-create the right-hand value with Temperature.FromCelcius/FromKelvin/FromFahrenheit before adding.
  2. Check the unit at ingestion (only 0=K, 1=C, 2=F are valid) and fail fast with a clear domain error.
  3. Perform the arithmetic on Kelvin doubles yourself and rebuild the result with FromKelvin if you must handle unknown units gracefully.

Example fix

// before
var sum = roomTemp + sensorReading; // sensorReading unit corrupt -> NotSupportedException
// after
var sum = roomTemp + Temperature.FromCelsius(sensorReading.Celsius.Value); // re-create with a valid unit
Defensive patterns

Strategy: validation

Validate before calling

if (HasKnownUnit(sensorReading))
    var sum = roomTemp.Add(sensorReading);
static bool HasKnownUnit(Temperature t)
{
    var f = typeof(Temperature).GetField("Type", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    int v = Convert.ToInt32(f!.GetValue(t));
    return v is 0 or 1 or 2;
}

Type guard

static bool CanAdd(Temperature lhs, Temperature rhs)
{
    try { var _ = rhs.ToString(); return true; }
    catch (NotSupportedException) { return false; }
}

Try / catch

Temperature sum;
try { sum = roomTemp + sensorReading; }
catch (NotSupportedException) { sum = Temperature.FromCelsius(roomTemp.Celsius.Value + ConvertToCelsius(sensorReading)); }

Prevention

When it happens

Trigger: Calling Add(rhs) or using the + operator where the left Temperature is Celsius and the right Temperature's internal UnitType is an out-of-range value (default/deserialized struct).

Common situations: Physics/HVAC-style code summing readings persisted by another system with different enum encodings; unit conversion pipelines feeding corrupt values into arithmetic.

Related errors


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

Appendix: source

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

            _ => throw new NotSupportedException(Type.ToString())
        };

    public Temperature Add(Temperature rhs) =>
        Type switch
        {
            UnitType.K => rhs.Type switch
                          {
                              UnitType.K => new Temperature(UnitType.K, Value + rhs.Value),
                              UnitType.C => new Temperature(UnitType.K, Value + CtoK(rhs.Value)),
                              UnitType.F => new Temperature(UnitType.K, Value + FtoK(rhs.Value)),
                              _          => throw new NotSupportedException(Type.ToString())
                          },
            UnitType.C => rhs.Type switch
                          {
                              UnitType.K => new Temperature(UnitType.C, Value + KtoC(rhs.Value)),
                              UnitType.C => new Temperature(UnitType.C, Value + rhs.Value),
                              UnitType.F => new Temperature(UnitType.C, Value + FtoC(rhs.Value)),
                              _          => throw new NotSupportedException(Type.ToString())
                          },
            UnitType.F => rhs.Type switch
                          {
                              UnitType.K => new Temperature(UnitType.F, Value + KtoF(rhs.Value)),
                              UnitType.C => new Temperature(UnitType.F, Value + CtoF(rhs.Value)),
                              UnitType.F => new Temperature(UnitType.F, Value + rhs.Value),
                              _          => throw new NotSupportedException(Type.ToString())
                          },
            _ => throw new NotSupportedException(Type.ToString())
        }; 

    public Temperature Add(double rhs) =>
        new (Type, Value + rhs);
    
    public Temperature Subtract(Temperature rhs) =>
        Type switch
        {
            UnitType.K => rhs.Type switch

View on GitHub (pinned to 2f0e362824)