Enhance error handling in ScriptInjector for injection and removal processes
All checks were successful
Auto Release Plugin / build-and-release (push) Successful in 55s

This commit is contained in:
CodeDevMLH
2026-01-06 23:22:59 +01:00
parent 6c131aef58
commit e3213f72cc

View File

@@ -37,7 +37,6 @@ namespace Jellyfin.Plugin.MediaBarEnhanced
/// <summary> /// <summary>
/// Injects the script tag into index.html if it's not already present. /// Injects the script tag into index.html if it's not already present.
/// </summary> /// </summary>
/// <returns>True if injection was successful or already present, false otherwise.</returns>
public void Inject() public void Inject()
{ {
try try
@@ -101,6 +100,11 @@ namespace Jellyfin.Plugin.MediaBarEnhanced
_logger.LogInformation("MediaBarEnhanced script and CSS already present in index.html. Or could not be injected."); _logger.LogInformation("MediaBarEnhanced script and CSS already present in index.html. Or could not be injected.");
} }
} }
catch (UnauthorizedAccessException)
{
_logger.LogWarning("Unauthorized access when attempting to inject script into index.html. Automatic injection failed. Attempting fallback now...");
RegisterFileTransformation();
}
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error injecting MediaBarEnhanced resources. Attempting fallback."); _logger.LogError(ex, "Error injecting MediaBarEnhanced resources. Attempting fallback.");
@@ -148,8 +152,15 @@ namespace Jellyfin.Plugin.MediaBarEnhanced
{ {
File.WriteAllText(indexPath, content); File.WriteAllText(indexPath, content);
_logger.LogInformation("MediaBarEnhanced script removed from index.html."); _logger.LogInformation("MediaBarEnhanced script removed from index.html.");
} else
{
_logger.LogInformation("MediaBarEnhanced script not found in index.html. No removal necessary.");
} }
} }
catch (UnauthorizedAccessException uaEx)
{
_logger.LogError(uaEx, "Unauthorized access when trying to remove MediaBarEnhanced script. Check file permissions.");
}
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error removing MediaBarEnhanced script."); _logger.LogError(ex, "Error removing MediaBarEnhanced script.");
@@ -170,7 +181,6 @@ namespace Jellyfin.Plugin.MediaBarEnhanced
{ {
JObject payload = new JObject(); JObject payload = new JObject();
// Random GUID for ID
payload.Add("id", "0dfac9d7-d898-4944-900b-1c1837707279"); payload.Add("id", "0dfac9d7-d898-4944-900b-1c1837707279");
payload.Add("fileNamePattern", "index.html"); payload.Add("fileNamePattern", "index.html");
payload.Add("callbackAssembly", GetType().Assembly.FullName); payload.Add("callbackAssembly", GetType().Assembly.FullName);
@@ -221,7 +231,6 @@ namespace Jellyfin.Plugin.MediaBarEnhanced
if (pluginInterfaceType != null) if (pluginInterfaceType != null)
{ {
// The ID must match the one used in RegisterFileTransformation
Guid id = Guid.Parse("0dfac9d7-d898-4944-900b-1c1837707279"); Guid id = Guid.Parse("0dfac9d7-d898-4944-900b-1c1837707279");
pluginInterfaceType.GetMethod("RemoveTransformation")?.Invoke(null, new object?[] { id }); pluginInterfaceType.GetMethod("RemoveTransformation")?.Invoke(null, new object?[] { id });
_logger.LogInformation("File transformation unregistered successfully."); _logger.LogInformation("File transformation unregistered successfully.");
@@ -230,7 +239,6 @@ namespace Jellyfin.Plugin.MediaBarEnhanced
} }
catch (Exception ex) catch (Exception ex)
{ {
// Log but don't throw, as we want to continue with normal removal
_logger.LogWarning(ex, "Error attempting to unregister file transformation. It might not have been registered."); _logger.LogWarning(ex, "Error attempting to unregister file transformation. It might not have been registered.");
} }
} }