From 074128967501977d2512f6056d023ccde5225d4c Mon Sep 17 00:00:00 2001 From: Steven Cohn Date: Sun, 25 Jul 2021 16:31:12 -0400 Subject: [PATCH] catch theme loading exceptions --- OneMore/Colorizer/Colorizer.cs | 45 ++++++++++++++++++++++++++++------ OneMore/Colorizer/Provider.cs | 12 +++++++-- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/OneMore/Colorizer/Colorizer.cs b/OneMore/Colorizer/Colorizer.cs index a1448cdfa6..08a08e8367 100644 --- a/OneMore/Colorizer/Colorizer.cs +++ b/OneMore/Colorizer/Colorizer.cs @@ -72,13 +72,27 @@ public XElement Colorize(string source, XNamespace ns) { // plain text prior to capture code = code.Replace("\t", " "); - var style = theme.GetStyle("plaintext"); - builder.Append(style == null ? code : style.Apply(code)); + if (theme != null) + { + var style = theme.GetStyle("plaintext"); + builder.Append(style == null ? code : style.Apply(code)); + } + else + { + builder.Append(code); + } } else { - var style = theme.GetStyle(scope); - builder.Append(style == null ? code : style.Apply(code)); + if (theme != null) + { + var style = theme.GetStyle(scope); + builder.Append(style == null ? code : style.Apply(code)); + } + else + { + builder.Append(code); + } } } }); @@ -113,13 +127,28 @@ public string ColorizeOne(string source) // plain text prior to capture // simple conversion of tabs to spaces (shouldn't be tabs in OneNote) code = code.Replace("\t", " "); - var style = theme.GetStyle("plaintext"); - builder.Append(style == null ? code : style.Apply(code)); + + if (theme != null) + { + var style = theme.GetStyle("plaintext"); + builder.Append(style == null ? code : style.Apply(code)); + } + else + { + builder.Append(code); + } } else { - var style = theme.GetStyle(scope); - builder.Append(style == null ? code : style.Apply(code)); + if (theme != null) + { + var style = theme.GetStyle(scope); + builder.Append(style == null ? code : style.Apply(code)); + } + else + { + builder.Append(code); + } } } }); diff --git a/OneMore/Colorizer/Provider.cs b/OneMore/Colorizer/Provider.cs index a477db7ce1..f89bc7eafa 100644 --- a/OneMore/Colorizer/Provider.cs +++ b/OneMore/Colorizer/Provider.cs @@ -73,9 +73,17 @@ public static ITheme LoadTheme(string path) { var json = File.ReadAllText(path); var serializer = new JavaScriptSerializer(); - var theme = serializer.Deserialize(json); + Theme theme = null; - theme.TranslateColorNames(); + try + { + theme = serializer.Deserialize(json); + theme.TranslateColorNames(); + } + catch (Exception exc) + { + Logger.Current.WriteLine($"error loading theme {path}", exc); + } return theme; }