diff --git a/Jellyfin.Plugin.Themerr/ThemerrManager.cs b/Jellyfin.Plugin.Themerr/ThemerrManager.cs index 10c8a3e7..50b2f274 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,12 +164,16 @@ public Task DownloadAllThemerr() _logger.LogInformation("{movieName} theme song not in database, or no internet connection", movie.Name); } - } - catch (Exception) + catch (HttpRequestException e) + { + _logger.LogInformation("{movieName} theme song not in database, or no internet connection: {error}", + movie.Name, e); + } + catch (Exception e) { - _logger.LogInformation("{movieName} theme song not in database, or no internet connection", - movie.Name); + _logger.LogInformation("Unknown exception occurred for {movieName}: {error}", + movie.Name, e); } } return Task.CompletedTask;