Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Commit

Permalink
improve json error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Mar 2, 2023
1 parent 5d9206b commit 84d5677
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions Jellyfin.Plugin.Themerr/ThemerrManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 84d5677

Please sign in to comment.