From 9d37e56f3ff3020049750e48dca33f4159631b61 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Wed, 1 Mar 2023 18:54:45 -0500 Subject: [PATCH] improve json error handling --- Jellyfin.Plugin.Themerr/ThemerrManager.cs | 47 ++++++++++++++++++----- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/Jellyfin.Plugin.Themerr/ThemerrManager.cs b/Jellyfin.Plugin.Themerr/ThemerrManager.cs index 10c8a3e..760d901 100644 --- a/Jellyfin.Plugin.Themerr/ThemerrManager.cs +++ b/Jellyfin.Plugin.Themerr/ThemerrManager.cs @@ -82,11 +82,30 @@ public Task DownloadAllThemerr() var existingYoutubeThemeUrl = ""; if (System.IO.File.Exists(themerrDataPath)) { - var jsonString = System.IO.File.ReadAllText(themerrDataPath); - dynamic jsonData = JsonConvert.DeserializeObject(jsonString); - if (jsonData != null) + // handle errors, delete file if it is corrupted + try { - existingYoutubeThemeUrl = jsonData.youtube_theme_url; + var jsonString = System.IO.File.ReadAllText(themerrDataPath); + dynamic jsonData = JsonConvert.DeserializeObject(jsonString); + if (jsonData != null) + { + existingYoutubeThemeUrl = jsonData.youtube_theme_url; + } + } + catch (JsonSerializationException e) + { + _logger.LogError("Deleting corrupted file `{themerrDataPath}`: {error}", + themerrDataPath, e); + // delete the file + try + { + System.IO.File.Delete(themerrDataPath); + } + catch (Exception exception) + { + _logger.LogError("Failed to delete corrupted file `{themerrDataPath}`: {error}", + themerrDataPath, exception); + } } } @@ -121,14 +140,15 @@ public Task DownloadAllThemerr() SaveMp3(themePath, youtubeThemeUrl); _logger.LogInformation("{movieName} theme song successfully downloaded", movie.Name); - // create themerr.json (json object) with these keys, youtube_theme_url, downloaded_timestamp + // create themerr.json (json object) with keys, youtube_theme_url, downloaded_timestamp var themerrData = new { downloaded_timestamp = DateTime.UtcNow, youtube_theme_url = youtubeThemeUrl }; // write themerr.json to disk - System.IO.File.WriteAllText(themerrDataPath, JsonConvert.SerializeObject(themerrData)); + System.IO.File.WriteAllText(themerrDataPath, + JsonConvert.SerializeObject(themerrData)); // update the metadata movie.RefreshMetadata(CancellationToken.None); @@ -144,13 +164,22 @@ public Task DownloadAllThemerr() _logger.LogInformation("{movieName} theme song not in database, or no internet connection", movie.Name); } - } - catch (Exception) + catch (HttpRequestException e) when ((int)e.StatusCode == 404) { - _logger.LogInformation("{movieName} theme song not in database, or no internet connection", + _logger.LogInformation("{movieName} theme song not in database.", movie.Name); } + catch (HttpRequestException e) + { + _logger.LogInformation("Network exception occurred for movie: {movieName}: {error}", + movie.Name, e); + } + catch (Exception e) + { + _logger.LogInformation("Unknown exception occurred for {movieName}: {error}", + movie.Name, e); + } } return Task.CompletedTask; }