{"record":{"id":"06773f79d218135a","repo":"SubtitleEdit/subtitleedit","slug":"parse-error-in-event-i-1-style-ex-message","errorCode":null,"errorMessage":"Parse error in event #{i + 1} style: {ex.Message}","messagePattern":"Parse error in event #(.+?) style: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/libse/SubtitleFormats/ClqttJson.cs","lineNumber":145,"sourceCode":"                            foreach (var styleJson in styles)\n                            {\n                                var styleType = parser.GetFirstObject(styleJson, \"type\");\n                                if (styleType.Equals(\"italic\", StringComparison.InvariantCultureIgnoreCase))\n                                {\n                                    try\n                                    {\n                                        var from = int.Parse(parser.GetFirstObject(styleJson, \"from\"));\n                                        var to = int.Parse(parser.GetFirstObject(styleJson, \"to\"));\n\n                                        subText = subText.Substring(0, from)\n                                                    + \"<i>\"\n                                                    + subText.Substring(from, to - from)\n                                                    + \"</i>\"\n                                                    + subText.Substring(to);\n                                    }\n                                    catch (Exception ex)\n                                    {\n                                        throw new InvalidOperationException($\"Parse error in event #{i + 1} style: {ex.Message}\", ex);\n                                    }\n                                }\n                            }\n\n                            var subTextPrefix = string.Empty;\n                            if (region.Equals(\"top\", StringComparison.InvariantCultureIgnoreCase))\n                            {\n                                subTextPrefix = \"{\\\\an8}\";\n                            }\n\n                            subText = subText.Replace(\"\\r\\n\", Environment.NewLine).Replace(\"\\n\", Environment.NewLine);\n                            subText = subTextPrefix + Json.DecodeJsonText(subText);\n\n                            var p = new Paragraph(subText, FramesToMilliseconds(start, frameRate), FramesToMilliseconds(end, frameRate));\n                            p.Region = region;\n\n                            if (annotations.Count > 0)\n                            {","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/libse/SubtitleFormats/ClqttJson.cs#L127-L163","documentation":"Wraps an exception raised while parsing the inline 'styles' array of a CLQTT event — specifically when converting the 'from'/'to' character offsets to int (int.Parse) fails, or the substring slicing based on those offsets is out of range. The inner exception message is embedded in the thrown InvalidOperationException.","triggerScenarios":"A CLQTT event whose styles[].from or styles[].to fields are non-numeric, empty, or where 'to' < 'from', causing int.Parse or the subsequent Substring calls to throw. The 1-based event index is included to locate the offending entry.","commonSituations":"Malformed style offsets from a buggy exporter; CLQTT files edited by hand with wrong index values; text that was shortened after style offsets were computed (stale offsets).","solutions":["Inspect event #{i+1} in the .clqtt file and verify each style's 'from' and 'to' are valid integers within the text length and from <= to.","Remove or regenerate the style entries for the offending event.","If producing CLQTT, recompute style offsets after any text edit."],"exampleFix":"// before\n\"styles\": [{ \"from\": \"abc\", \"to\": \"5\" }]\n// after\n\"styles\": [{ \"from\": 0, \"to\": 5 }]","handlingStrategy":"try-catch","validationCode":"// Validate style offsets against the text length before parsing\nvar txt = parser.GetFirstObject(eventJson, \"txt\");\nforeach (var styleJson in styles)\n{\n    if (!int.TryParse(parser.GetFirstObject(styleJson, \"from\"), out var from)\n        || !int.TryParse(parser.GetFirstObject(styleJson, \"to\"), out var to)\n        || from < 0 || to > txt.Length || from > to)\n    {\n        // skip or fix the malformed style\n        continue;\n    }\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    new ClqttJson().LoadSubtitle(sub, lines, fileName);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Parse error in event\") && ex.Message.Contains(\"style\"))\n{\n    // ex.Message includes the event number and inner cause\n    logger.Error(ex, \"CLQTT style parse failed\");\n}","preventionTips":["Recompute style from/to offsets whenever the event text is edited.","Validate offsets are integers within text bounds before writing CLQTT.","Use a CLQTT exporter that recomputes offsets atomically."],"tags":["subtitle-format","clqtt","json","style","index-out-of-range"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}