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

Commit

Permalink
fix: add exception handling to json read
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed May 22, 2024
1 parent 0dc949c commit 5cb8894
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
8 changes: 8 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
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"
},
35 changes: 29 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,31 @@ public string CreateThemerrDbLink(string tmdbId, string dbType)
public string GetYoutubeThemeUrl(string themerrDbUrl, BaseItem item)
{
var client = new HttpClient();
string jsonString;

try
{
var jsonString = client.GetStringAsync(themerrDbUrl).Result;
dynamic jsonData = JsonConvert.DeserializeObject(jsonString);
return jsonData?.youtube_theme_url;
jsonString = client.GetStringAsync(themerrDbUrl).Result;
}
catch (HttpRequestException e) when (e.StatusCode == HttpStatusCode.NotFound)
{

Check warning on line 359 in Jellyfin.Plugin.Themerr/ThemerrManager.cs

View check run for this annotation

Codecov / codecov/patch

Jellyfin.Plugin.Themerr/ThemerrManager.cs#L358-L359

Added lines #L358 - L359 were not covered by tests
#pragma warning disable S6667
_logger.LogWarning(
"ThemerrDB entry not found (404): {ItemName}, contribute:\n {IssueUrl}",
item.Name,
GetIssueUrl(item));

Check warning on line 364 in Jellyfin.Plugin.Themerr/ThemerrManager.cs

View check run for this annotation

Codecov / codecov/patch

Jellyfin.Plugin.Themerr/ThemerrManager.cs#L361-L364

Added lines #L361 - L364 were not covered by tests
#pragma warning restore S6667

return string.Empty;

Check warning on line 367 in Jellyfin.Plugin.Themerr/ThemerrManager.cs

View check run for this annotation

Codecov / codecov/patch

Jellyfin.Plugin.Themerr/ThemerrManager.cs#L367

Added line #L367 was not covered by tests
}
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;
}

dynamic jsonData = JsonConvert.DeserializeObject(jsonString);
return jsonData?.youtube_theme_url;
}

/// <summary>
Expand Down

0 comments on commit 5cb8894

Please sign in to comment.