{"record":{"id":"2d57095b28471680","repo":"AvaloniaUI/Avalonia","slug":"invalid-unit-part-tostring","errorCode":null,"errorMessage":"Invalid unit: {part.ToString()}","messagePattern":"Invalid unit: (.+?)","errorType":"validation","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Media/Transformation/TransformParser.cs","lineNumber":374,"sourceCode":"            throw new FormatException($\"Invalid value {value.Value} {unitString} for {function}\");\n        }\n\n        private static void ThrowFormatInvalidValueCount(TransformFunction function, int count)\n        {\n            throw new FormatException($\"Invalid format. {function} expects {count} value(s).\");\n        }\n\n        private static Unit ParseUnit(in ReadOnlySpan<char> part)\n        {\n            foreach (var (name, unit) in s_unitMapping)\n            {\n                if (part.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase))\n                {\n                    return unit;\n                }\n            }\n\n            throw new FormatException($\"Invalid unit: {part.ToString()}\");\n        }\n\n        private static TransformFunction ParseTransformFunction(in ReadOnlySpan<char> part)\n        {\n            foreach (var (name, transformFunction) in s_functionMapping)\n            {\n                if (part.Equals(name.AsSpan(), StringComparison.OrdinalIgnoreCase))\n                {\n                    return transformFunction;\n                }\n            }\n\n            return TransformFunction.Invalid;\n        }\n\n        private static double ToRadians(in UnitValue value)\n        {\n            return value.Unit switch","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Media/Transformation/TransformParser.cs#L356-L392","documentation":"Thrown by TransformParser.ParseUnit when a unit token does not match any of the supported units. The recognized units are deg, grad, rad, turn, and px (mapped in s_unitMapping). Any other unit string — or a value with an unrecognized suffix — causes this FormatException, which echoes the offending token.","triggerScenarios":"Supplying a value with a unit suffix not in {deg, grad, rad, turn, px}, e.g. '10em', '5pt', '2%', or a bare unknown token. The ParseUnit loop finds no match and throws.","commonSituations":"Using CSS units Avalonia's transform parser does not support (em, rem, %, pt, vw); typos in unit names ('degress'); assuming browser-CSS unit coverage that the parser does not implement.","solutions":["Use only supported units: px for lengths, deg/grad/rad/turn for angles.","Convert unsupported units (em, %, pt) to px before building the transform string.","Check the echoed '{part}' token in the message to find the offending unit."],"exampleFix":"// before\nvar t = TransformOperations.Parse(\"translate(2em, 1em)\"); // 'em' unsupported\n\n// after\nvar t = TransformOperations.Parse(\"translate(32px, 16px)\");","handlingStrategy":"validation","validationCode":"static readonly HashSet<string> s_validUnits =\n    new() { \"deg\", \"grad\", \"rad\", \"turn\", \"px\", \"\" };\n\nstatic bool HasValidUnits(string transform)\n    /* check each numeric token's unit suffix against s_validUnits */;","typeGuard":null,"tryCatchPattern":"try { return TransformOperations.Parse(s); }\ncatch (FormatException ex) when (ex.Message.Contains(\"Invalid unit\"))\n{ /* convert unsupported unit (em, %, pt) to px and retry */ }","preventionTips":["Restrict transform units to px (length) and deg/grad/rad/turn (angle).","Convert CSS-only units (em, rem, %, pt) to px before parsing.","Inspect the offending unit token in the exception message."],"tags":["transform","parse","format-exception","unit-validation","avalonia"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}