{"record":{"id":"b8579b44f78594d9","repo":"LykosAI/StabilityMatrix","slug":"specified-argument-was-out-of-range-of-valid-values","errorCode":null,"errorMessage":"Specified argument was out of range of valid values. (Parameter 'NanHandling')","messagePattern":"Specified argument was out of range of valid values\\. \\(Parameter 'NanHandling'\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Converters/NullableDefaultNumericConverter.cs","lineNumber":36,"sourceCode":"    /// <summary>\n    /// Unboxes a nullable value type\n    /// </summary>\n    private TSource Unbox(TTarget? value)\n    {\n        if (!value.HasValue)\n        {\n            return default;\n        }\n\n        if (TTarget.IsNaN(value.Value))\n        {\n            return NanHandling switch\n            {\n                ReturnBehavior.DefaultValue => default,\n                ReturnBehavior.Throw\n                    => throw new InvalidCastException(\"Cannot convert NaN to a numeric type\"),\n                _\n                    => throw new InvalidEnumArgumentException(\n                        nameof(NanHandling),\n                        (int)NanHandling,\n                        typeof(ReturnBehavior)\n                    )\n            };\n        }\n\n        return (TSource)System.Convert.ChangeType(value.Value, typeof(TSource));\n    }\n\n    /// <summary>\n    /// Convert a value type to a nullable value type\n    /// </summary>\n    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)\n    {\n        if (targetType != typeof(TTarget?) && !targetType.IsAssignableTo(typeof(TTarget)))\n        {\n            // ReSharper disable once LocalizableElement","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Converters/NullableDefaultNumericConverter.cs#L18-L54","documentation":"In the same Unbox NaN path, the switch's default arm assumes any NanHandling other than DefaultValue/Throw is an InvalidEnumArgumentException with parameter 'NanHandling'. Hitting it with message \"Specified argument was out of range of valid values. (Parameter 'NanHandling')\" means NanHandling held an undefined enum value (out-of-range cast or corrupt state).","triggerScenarios":"NanHandling set to an int not defined in ReturnBehavior (e.g. via XAML attribute parsing, reflection, or a bad cast), causing the switch to fall into the default arm.","commonSituations":"Binding a numeric XAML attribute to ReturnBehavior with an invalid value; a versioned enum change making stored settings reference a removed member; constructing the converter with (ReturnBehavior)99.","solutions":["Set NanHandling to a valid ReturnBehavior member (DefaultValue or Throw)","Validate enum values in XAML (use x:Static instead of raw ints)","Add an Enum.IsDefined check when constructing the converter from untrusted input","Update persisted settings that reference a renamed/removed enum member"],"exampleFix":"// before\n<conv:NullableDefaultNumericConverter NanHandling=\"3\"/>\n// after\n<conv:NullableDefaultNumericConverter NanHandling=\"DefaultValue\"/>","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(ReturnBehavior), nanHandling))\n    nanHandling = ReturnBehavior.DefaultValue;","typeGuard":"bool IsValidReturnBehavior(ReturnBehavior b) => b is ReturnBehavior.DefaultValue or ReturnBehavior.Throw;","tryCatchPattern":"try { result = converter.ConvertBack(v, t, p, c); }\ncatch (InvalidEnumArgumentException ex) { Log.Error(ex, \"Invalid NanHandling\"); result = 0d; }","preventionTips":["Set enum options via XAML member names or x:Static, never raw ints","Migrate persisted settings when ReturnBehavior members change","Validate enum values with Enum.IsDefined when built from untrusted input"],"tags":["avalonia","converter","enum","invalid-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}