{"record":{"id":"9dd22045b07c9abb","repo":"louthy/language-ext","slug":"notsupportedexception-type-tostring","errorCode":null,"errorMessage":"NotSupportedException: Type.ToString()","messagePattern":"NotSupportedException: Type\\.ToString\\(\\)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Units of Measure/Temperature.cs","lineNumber":190,"sourceCode":"            _ => throw new NotSupportedException(Type.ToString())\n        };\n\n    public Temperature Add(Temperature rhs) =>\n        Type switch\n        {\n            UnitType.K => rhs.Type switch\n                          {\n                              UnitType.K => new Temperature(UnitType.K, Value + rhs.Value),\n                              UnitType.C => new Temperature(UnitType.K, Value + CtoK(rhs.Value)),\n                              UnitType.F => new Temperature(UnitType.K, Value + FtoK(rhs.Value)),\n                              _          => throw new NotSupportedException(Type.ToString())\n                          },\n            UnitType.C => rhs.Type switch\n                          {\n                              UnitType.K => new Temperature(UnitType.C, Value + KtoC(rhs.Value)),\n                              UnitType.C => new Temperature(UnitType.C, Value + rhs.Value),\n                              UnitType.F => new Temperature(UnitType.C, Value + FtoC(rhs.Value)),\n                              _          => throw new NotSupportedException(Type.ToString())\n                          },\n            UnitType.F => rhs.Type switch\n                          {\n                              UnitType.K => new Temperature(UnitType.F, Value + KtoF(rhs.Value)),\n                              UnitType.C => new Temperature(UnitType.F, Value + CtoF(rhs.Value)),\n                              UnitType.F => new Temperature(UnitType.F, Value + rhs.Value),\n                              _          => throw new NotSupportedException(Type.ToString())\n                          },\n            _ => throw new NotSupportedException(Type.ToString())\n        }; \n\n    public Temperature Add(double rhs) =>\n        new (Type, Value + rhs);\n    \n    public Temperature Subtract(Temperature rhs) =>\n        Type switch\n        {\n            UnitType.K => rhs.Type switch","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Units of Measure/Temperature.cs#L172-L208","documentation":"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().","triggerScenarios":"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).","commonSituations":"Physics/HVAC-style code summing readings persisted by another system with different enum encodings; unit conversion pipelines feeding corrupt values into arithmetic.","solutions":["Re-create the right-hand value with Temperature.FromCelcius/FromKelvin/FromFahrenheit before adding.","Check the unit at ingestion (only 0=K, 1=C, 2=F are valid) and fail fast with a clear domain error.","Perform the arithmetic on Kelvin doubles yourself and rebuild the result with FromKelvin if you must handle unknown units gracefully."],"exampleFix":"// before\nvar sum = roomTemp + sensorReading; // sensorReading unit corrupt -> NotSupportedException\n// after\nvar sum = roomTemp + Temperature.FromCelsius(sensorReading.Celsius.Value); // re-create with a valid unit","handlingStrategy":"validation","validationCode":"if (HasKnownUnit(sensorReading))\n    var sum = roomTemp.Add(sensorReading);\nstatic bool HasKnownUnit(Temperature t)\n{\n    var f = typeof(Temperature).GetField(\"Type\", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);\n    int v = Convert.ToInt32(f!.GetValue(t));\n    return v is 0 or 1 or 2;\n}","typeGuard":"static bool CanAdd(Temperature lhs, Temperature rhs)\n{\n    try { var _ = rhs.ToString(); return true; }\n    catch (NotSupportedException) { return false; }\n}","tryCatchPattern":"Temperature sum;\ntry { sum = roomTemp + sensorReading; }\ncatch (NotSupportedException) { sum = Temperature.FromCelsius(roomTemp.Celsius.Value + ConvertToCelsius(sensorReading)); }","preventionTips":["Validate the unit enum value (0/1/2) when data crosses a serialization, network, or DB boundary.","Convert every incoming reading to a known unit before combining it with in-memory values.","Add unit tests covering factory -> persist -> load -> arithmetic round-trips."],"tags":["unsupported-operation","units-of-measure","arithmetic","temperature"],"backgroundTag":"unsupported-enum-value","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}