{"record":{"id":"90502bea80f1fecb","repo":"babalae/better-genshin-impact","slug":"fieldname-1-4","errorCode":null,"errorMessage":"{fieldName} 必须是 1 到 4 个数字","messagePattern":"(.+?) 必须是 1 到 4 个数字","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Core/Recognition/RecognitionObjectJsonLoader.cs","lineNumber":490,"sourceCode":"\n        private static Rect BuildRect(object? x, object? y, object? width, object? height)\n        {\n            return new Rect(\n                (int)Math.Round(ToDouble(x)),\n                (int)Math.Round(ToDouble(y)),\n                (int)Math.Round(ToDouble(width)),\n                (int)Math.Round(ToDouble(height)));\n        }\n\n        private static Scalar BuildScalar(IReadOnlyList<double> values, string fieldName)\n        {\n            return values.Count switch\n            {\n                1 => new Scalar(values[0]),\n                2 => new Scalar(values[0], values[1]),\n                3 => new Scalar(values[0], values[1], values[2]),\n                4 => new Scalar(values[0], values[1], values[2], values[3]),\n                _ => throw new InvalidOperationException($\"{fieldName} 必须是 1 到 4 个数字\"),\n            };\n        }\n\n        private static TEnum ParseEnumExact<TEnum>(string? value, string fieldName) where TEnum : struct, Enum\n        {\n            if (string.IsNullOrWhiteSpace(value))\n            {\n                throw new InvalidOperationException($\"{fieldName} 不能为空\");\n            }\n\n            if (Enum.TryParse<TEnum>(value, false, out var parsed))\n            {\n                return parsed;\n            }\n\n            throw new InvalidOperationException($\"{fieldName} 的值 {value} 不是有效的 {typeof(TEnum).Name}\");\n        }\n","sourceCodeStart":472,"sourceCodeEnd":508,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Recognition/RecognitionObjectJsonLoader.cs#L472-L508","documentation":"BuildScalar constructs an OpenCvSharp Scalar for the lowerColor/upperColor color-range filters and supports only 1 to 4 channel values. If the JSON array is empty or has more than four entries, InvalidOperationException is thrown because a Scalar cannot represent that many channels.","triggerScenarios":"A RecognitionObject has \"lowerColor\": [] or \"lowerColor\": [0,0,0,0,0] (five values) in Recognition.json.","commonSituations":"Misunderstood channel count for the colorspace; accidentally added an alpha channel; pasted a 5+ element array from another tool.","solutions":["Provide exactly 1 to 4 numeric values for lowerColor/upperColor.","Match the element count to the color space (e.g. 3 for BGR).","Validate the array length against the Scalar channel limit before runtime."],"exampleFix":"// before\n// \"lowerColor\": [100, 100, 100, 100, 100]  -> throws\n\n// after\n// \"lowerColor\": [100, 100, 100]","handlingStrategy":"validation","validationCode":"static void AssertScalarShape(IReadOnlyList<double> values, string fieldName)\n{\n    if (values.Count is < 1 or > 4)\n        throw new InvalidOperationException($\"{fieldName} must have 1-4 values, got {values.Count}.\");\n}\n\nAssertScalarShape(config.LowerColor, nameof(config.LowerColor));\nAssertScalarShape(config.UpperColor, nameof(config.UpperColor));","typeGuard":"static bool IsValidScalar(IReadOnlyList<double> values) => values.Count is >= 1 and <= 4;","tryCatchPattern":"try { return RecognitionObjectJsonLoader.Load(config, objectName, context); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"必须是 1 到 4 个数字\"))\n{ Logger.LogError(ex, \"Color scalar has invalid channel count.\"); throw; }","preventionTips":["Match color array length to the colorspace (1 grayscale, 3 BGR).","Validate lowerColor/upperColor lengths in authoring tooling.","Avoid adding an alpha channel to BGR ranges."],"tags":["config","color","opencv","scalar","validation"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}