From 4a2b05da229fe68560ffdcf42dc73f22d6ee6795 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sun, 9 Jun 2024 12:00:43 -0400 Subject: [PATCH] fix: add exception handling to json read (#331) --- .../TestThemerrManager.cs | 12 +++++++ .../data/invalid.json | 5 +++ Jellyfin.Plugin.Themerr/ThemerrManager.cs | 34 +++++++++++++++---- 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 Jellyfin.Plugin.Themerr.Tests/data/invalid.json diff --git a/Jellyfin.Plugin.Themerr.Tests/TestThemerrManager.cs b/Jellyfin.Plugin.Themerr.Tests/TestThemerrManager.cs index 406d040..86d7e07 100644 --- a/Jellyfin.Plugin.Themerr.Tests/TestThemerrManager.cs +++ b/Jellyfin.Plugin.Themerr.Tests/TestThemerrManager.cs @@ -80,6 +80,14 @@ private void TestGetExistingThemerrDataValue() Assert.Null(_themerrManager.GetExistingThemerrDataValue("any_key", themerrDataPath)); + // ensure null when the json is invalid + themerrDataPath = Path.Combine( + Directory.GetCurrentDirectory(), + "data", + "invalid.json"); + + Assert.Null(_themerrManager.GetExistingThemerrDataValue("any_key", themerrDataPath)); + // test empty json file themerrDataPath = Path.Combine( Directory.GetCurrentDirectory(), @@ -460,6 +468,10 @@ private void TestGetYoutubeThemeUrlExceptions(BaseItem item) var tmdbId = _themerrManager.GetTmdbId(item); var themerrDbLink = _themerrManager.CreateThemerrDbLink(tmdbId, dbType); + // log + TestLogger.Info($"tmdbId: {tmdbId}"); + TestLogger.Info($"themerrDbLink: {themerrDbLink}"); + // get the new YouTube theme url var youtubeThemeUrl = _themerrManager.GetYoutubeThemeUrl(themerrDbLink, item); diff --git a/Jellyfin.Plugin.Themerr.Tests/data/invalid.json b/Jellyfin.Plugin.Themerr.Tests/data/invalid.json new file mode 100644 index 0000000..a28aba5 --- /dev/null +++ b/Jellyfin.Plugin.Themerr.Tests/data/invalid.json @@ -0,0 +1,5 @@ +{ + "downloaded_timestamp": "2023-12-12T04:07:41.7659077Z", + "youtube_theme_url": "https://www.youtube.com/watch?v=E8nxMWr2sr4", + "dummy_key": "dummy_value" +}, diff --git a/Jellyfin.Plugin.Themerr/ThemerrManager.cs b/Jellyfin.Plugin.Themerr/ThemerrManager.cs index b2e8d96..41fc0e7 100644 --- a/Jellyfin.Plugin.Themerr/ThemerrManager.cs +++ b/Jellyfin.Plugin.Themerr/ThemerrManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -68,8 +69,17 @@ public string GetExistingThemerrDataValue(string key, string themerrDataPath) } var jsonString = System.IO.File.ReadAllText(themerrDataPath); - dynamic jsonData = JsonConvert.DeserializeObject(jsonString); - return jsonData?[key]; + + try + { + dynamic jsonData = JsonConvert.DeserializeObject(jsonString); + return jsonData?[key]; + } + catch (JsonReaderException e) + { + _logger.LogError(e, "Unable to parse themerr data file: {ThemerrDataPath}\n{JsonString}", themerrDataPath, jsonString); + return null; + } } /// @@ -339,18 +349,30 @@ public string CreateThemerrDbLink(string tmdbId, string dbType) public string GetYoutubeThemeUrl(string themerrDbUrl, BaseItem item) { var client = new HttpClient(); + HttpResponseMessage response; try { - var jsonString = client.GetStringAsync(themerrDbUrl).Result; - dynamic jsonData = JsonConvert.DeserializeObject(jsonString); - return jsonData?.youtube_theme_url; + response = client.GetAsync(themerrDbUrl).GetAwaiter().GetResult(); } catch (Exception e) { - _logger.LogWarning(e, "Missing from ThemerrDB: {ItemTitle}, contribute:\n {IssueUrl}", item.Name, GetIssueUrl(item)); + _logger.LogError(e, "Error retrieving from ThemerrDB: {ItemName}", item.Name); + return string.Empty; + } + + if (response.StatusCode == HttpStatusCode.NotFound) + { + _logger.LogWarning( + "ThemerrDB entry not found (404): {ItemName}, contribute:\n {IssueUrl}", + item.Name, + GetIssueUrl(item)); return string.Empty; } + + var jsonString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + dynamic jsonData = JsonConvert.DeserializeObject(jsonString); + return jsonData?.youtube_theme_url; } ///