Skip to content

Commit

Permalink
fix: add exception handling to json read (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher authored Jun 9, 2024
1 parent 145d549 commit 4a2b05d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Jellyfin.Plugin.Themerr.Tests/TestThemerrManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions Jellyfin.Plugin.Themerr.Tests/data/invalid.json
Original file line number Diff line number Diff line change
@@ -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"
},
34 changes: 28 additions & 6 deletions Jellyfin.Plugin.Themerr/ThemerrManager.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}

/// <summary>
Expand Down Expand Up @@ -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;
}

/// <summary>
Expand Down

0 comments on commit 4a2b05d

Please sign in to comment.