{"record":{"id":"3809d0169d3fb9bd","repo":"wmjordan/PDFPatcher","slug":"error","errorCode":null,"errorMessage":"尺寸单位无效。","messagePattern":"尺寸单位无效。","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"App/Model/UnitConverter.cs","lineNumber":41,"sourceCode":"\t\t\tget => _Precision;\r\n\t\t\tset {\r\n\t\t\t\tif (value < 0 || value > 6) {\r\n\t\t\t\t\tthrow new ArgumentException(\"转换精度不能小于 0 或大于 6。\");\r\n\t\t\t\t}\r\n\t\t\t\t_Precision = value;\r\n\t\t\t\t_PreservedValue = (float)Math.Pow(0.1, _Precision);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tprivate string _Unit;\r\n\t\t///<summary>获取或指定转换使用的单位。</summary>\r\n\t\t[XmlAttribute(\"单位\")]\r\n\t\tpublic string Unit {\r\n\t\t\tget => _Unit;\r\n\t\t\tset {\r\n\t\t\t\tvar f = ValueHelper.MapValue(value, Constants.Units.Names, Constants.Units.Factors, 0);\r\n\t\t\t\tif (f == 0) {\r\n\t\t\t\t\tthrow new ArgumentException(\"尺寸单位无效。\");\r\n\t\t\t\t}\r\n\t\t\t\tUnitFactor = f;\r\n\t\t\t\t_Unit = value;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tpublic UnitConverter() {\r\n\t\t\tUnit = Constants.Units.CM;\r\n\t\t\tPrecision = 3;\r\n\t\t}\r\n\r\n\t\tinternal float FromPoint(float point) {\r\n\t\t\treturn (point < _PreservedValue && point >= 0) // preserve small fragment\r\n\t\t\t\t\t? point\r\n\t\t\t\t\t: (float)Math.Round(point / UnitFactor, _Precision);\r\n\t\t}\r\n\r\n\t\tinternal float ToPoint(float value) {\r","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Model/UnitConverter.cs#L23-L59","documentation":"The UnitConverter.Unit setter looks up the supplied unit string in Constants.Units.Names ([厘米/CM, 毫米/MM, 英寸/Inch, 点/Point]) via ValueHelper.MapValue, which returns the default (0) when no match is found. Because a factor of 0 would silently make every conversion produce 0, the setter treats factor==0 as an invalid unit and throws ArgumentException.","triggerScenarios":"Assigning UnitConverter.Unit a string that is not one of the four supported localized names (\"厘米\", \"毫米\", \"英寸\", \"点\"). Also triggered by deserialization of an XML options file whose '单位' attribute is misspelled, in the wrong language, or null/empty.","commonSituations":"Storing the English abbreviation ('cm','mm','in','pt') instead of the Chinese localized name expected by Constants.Units.Names; a locale mismatch in the options XML; a corrupted or hand-edited settings file.","solutions":["Use exactly one of the four recognized names: \"厘米\", \"毫米\", \"英寸\", or \"点\" (see Constants.Units.Names).","If you have an English unit code, map it to the localized name before assigning.","Validate the unit string against Constants.Units.Names before setting Unit.","Check the '单位' attribute in the XML info/options document for typos or encoding issues."],"exampleFix":"// before\nconverter.Unit = \"cm\"; // throws: not in the localized name table\n\n// after\nconverter.Unit = Constants.Units.CM; // \"厘米\"","handlingStrategy":"validation","validationCode":"string unit = MapEnglishToLocalized(unitCode);\nif (Array.IndexOf(Constants.Units.Names, unit) < 0)\n    unit = Constants.Units.CM;\nconverter.Unit = unit;","typeGuard":"static bool IsValidUnit(string u) => Array.IndexOf(Constants.Units.Names, u) >= 0;","tryCatchPattern":null,"preventionTips":["Use the Constants.Units.* constants (CM, MM, Inch, Point) instead of string literals.","Map English unit codes to localized names before assignment.","Validate the XML '单位' attribute against Constants.Units.Names."],"tags":["validation","unit-conversion","configuration","localization","argument"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}