{"record":{"id":"830db5a69ebb6799","repo":"AvaloniaUI/Avalonia","slug":"unknown-format-specifier","errorCode":null,"errorMessage":"Unknown format specifier","messagePattern":"Unknown format specifier","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Input/KeyGesture.cs","lineNumber":113,"sourceCode":"\n        /// <summary>\n        /// Returns the current KeyGesture as a string formatted according to the format string and appropriate IFormatProvider\n        /// </summary>\n        /// <param name=\"format\">The format to use. \n        /// <list type=\"bullet\">\n        /// <item><term>null or \"\" or \"g\"</term><description>The Invariant format, uses Enum.ToString() to format Keys.</description></item>\n        /// <item><term>\"p\"</term><description>Use platform specific formatting as registerd.</description></item>\n        /// </list></param>\n        /// <param name=\"formatProvider\">The IFormatProvider to use.  If null, uses the appropriate provider registered in the Avalonia Locator, or Invariant.</param>\n        /// <returns>The formatted string.</returns>\n        /// <exception cref=\"FormatException\">Thrown if the format string is not null, \"\", \"g\", or \"p\"</exception>\n        public string ToString(string? format, IFormatProvider? formatProvider)\n        {\n            var formatInfo = format switch\n            {\n                null or \"\" or \"g\" => KeyGestureFormatInfo.Invariant,\n                \"p\" => KeyGestureFormatInfo.GetInstance(formatProvider),\n                _ => throw new FormatException(\"Unknown format specifier\")\n            };\n\n            var s = StringBuilderCache.Acquire();\n\n            static void Plus(StringBuilder s)\n            {\n                if (s.Length > 0)\n                {\n                    s.Append(\"+\");\n                }\n            }\n\n            if (KeyModifiers.HasAllFlags(KeyModifiers.Control))\n            {\n                s.Append(formatInfo.Ctrl);\n            }\n\n            if (KeyModifiers.HasAllFlags(KeyModifiers.Shift))","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Input/KeyGesture.cs#L95-L131","documentation":"KeyGesture.ToString(format, provider) supports only the invariant format (null/\"\"/\"g\", uses Enum.ToString for keys) and the platform format (\"p\", uses a registered KeyGestureFormatInfo). Any other format string is unknown, so it throws FormatException, mirroring IFormattable conventions.","triggerScenarios":"Calling `keyGesture.ToString(\"x\")`, `ToString(\"D\")`, or any specifier other than null/\"\"/\"g\"/\"p\".","commonSituations":"Passing a numeric/standard .NET format string to a KeyGesture. UI code generically forwarding a format string from a formatter setting into ToString.","solutions":["Use null, \"\", \"g\", or \"p\" as the format specifier.","If you only need the invariant representation, call parameterless ToString().","Register a custom KeyGestureFormatInfo and use \"p\" for platform-specific output."],"exampleFix":"// before:\nvar s = gesture.ToString(\"D\", culture); // throws\n\n// after:\nvar s = gesture.ToString(\"g\", culture); // invariant\n// or platform-specific:\nvar s = gesture.ToString(\"p\", culture);","handlingStrategy":"validation","validationCode":"var s = (format is null or \"\" or \"g\" or \"p\")\n    ? gesture.ToString(format, provider)\n    : throw new FormatException(\"Unknown format specifier\");","typeGuard":"static bool IsKnownFormat(string? f) => f is null or \"\" or \"g\" or \"p\";","tryCatchPattern":"try { return gesture.ToString(fmt, provider); }\ncatch (FormatException) { return gesture.ToString(); // invariant fallback }","preventionTips":["Pass only null/\"\"/\"g\"/\"p\" to KeyGesture.ToString.","Use parameterless ToString for invariant output.","Don't forward generic .NET format strings into ToString."],"tags":["keygesture","format","input","formatexception"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}