From 4388515614e2bcca1ecd28f9b70b368f1150e450 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Thu, 30 May 2024 21:40:57 -0500 Subject: [PATCH] refactor: Address a few more SonarLint issues --- .../Processors/Config/TemplateConfigCreator.cs | 9 ++------- .../ErrorHandling/ConsoleExceptionHandler.cs | 10 ++++++++++ src/Recyclarr.Cli/Program.cs | 4 ++-- src/Recyclarr.Settings/SettingsProvider.cs | 14 ++------------ .../BaseCommandSetupIntegrationTest.cs | 2 +- .../Common/Extensions/DictionaryExtensionsTest.cs | 2 +- 6 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/Recyclarr.Cli/Processors/Config/TemplateConfigCreator.cs b/src/Recyclarr.Cli/Processors/Config/TemplateConfigCreator.cs index 806f27f1..da09f570 100644 --- a/src/Recyclarr.Cli/Processors/Config/TemplateConfigCreator.cs +++ b/src/Recyclarr.Cli/Processors/Config/TemplateConfigCreator.cs @@ -31,19 +31,14 @@ public class TemplateConfigCreator( { CopyTemplate(templateFile, settings); } - catch (FileExistsException e) - { - log.Error(e, "Template configuration file could not be saved"); - } catch (FileLoadException) { // Do not log here since the origin of this exception is ConfigParser.Load(), which already has // sufficient logging. } - catch (Exception e) + catch (IOException e) { - log.Error(e, "Unable to save configuration template file"); - throw; + log.Error(e, "Unable to save configuration template file; skipping"); } } } diff --git a/src/Recyclarr.Cli/Processors/ErrorHandling/ConsoleExceptionHandler.cs b/src/Recyclarr.Cli/Processors/ErrorHandling/ConsoleExceptionHandler.cs index def7fb62..e744293a 100644 --- a/src/Recyclarr.Cli/Processors/ErrorHandling/ConsoleExceptionHandler.cs +++ b/src/Recyclarr.Cli/Processors/ErrorHandling/ConsoleExceptionHandler.cs @@ -4,6 +4,7 @@ using Recyclarr.Compatibility; using Recyclarr.Config.ExceptionTypes; using Recyclarr.Config.Parsing.ErrorHandling; using Recyclarr.VersionControl; +using YamlDotNet.Core; namespace Recyclarr.Cli.Processors.ErrorHandling; @@ -59,6 +60,15 @@ public class ConsoleExceptionHandler(ILogger log, IFlurlHttpExceptionHandler htt log.Error(e.Message); break; + case YamlException e: + log.Error(e, "Exception while parsing settings.yml at line {Line}", e.Start.Line); + if (e.Data["ContextualMessage"] is string context) + { + log.Error(context); + } + + break; + default: return false; } diff --git a/src/Recyclarr.Cli/Program.cs b/src/Recyclarr.Cli/Program.cs index 1a7aeeb7..da4a72c1 100644 --- a/src/Recyclarr.Cli/Program.cs +++ b/src/Recyclarr.Cli/Program.cs @@ -8,7 +8,7 @@ namespace Recyclarr.Cli; internal static class Program { - public static int Main(string[] args) + public static async Task Main(string[] args) { var builder = new ContainerBuilder(); CompositionRoot.Setup(builder); @@ -43,6 +43,6 @@ internal static class Program CliSetup.Commands(config); }); - return app.Run(args); + return await app.RunAsync(args); } } diff --git a/src/Recyclarr.Settings/SettingsProvider.cs b/src/Recyclarr.Settings/SettingsProvider.cs index 57e9c3d8..269ecd9d 100644 --- a/src/Recyclarr.Settings/SettingsProvider.cs +++ b/src/Recyclarr.Settings/SettingsProvider.cs @@ -2,22 +2,19 @@ using System.IO.Abstractions; using Recyclarr.Common.Extensions; using Recyclarr.Platform; using Recyclarr.Yaml; -using Serilog; using YamlDotNet.Core; namespace Recyclarr.Settings; public class SettingsProvider : ISettingsProvider { - private readonly ILogger _log; private readonly IAppPaths _paths; private readonly Lazy _settings; public SettingsValues Settings => _settings.Value; - public SettingsProvider(ILogger log, IAppPaths paths, IYamlSerializerFactory serializerFactory) + public SettingsProvider(IAppPaths paths, IYamlSerializerFactory serializerFactory) { - _log = log; _paths = paths; _settings = new Lazy(() => LoadOrCreateSettingsFile(serializerFactory)); } @@ -34,14 +31,7 @@ public class SettingsProvider : ISettingsProvider } catch (YamlException e) { - _log.Error(e, "Exception while parsing settings.yml at line {Line}", e.Start.Line); - - var context = SettingsContextualMessages.GetContextualErrorFromException(e); - if (context is not null) - { - _log.Error(context); - } - + e.Data["ContextualMessage"] = SettingsContextualMessages.GetContextualErrorFromException(e); throw; } } diff --git a/tests/Recyclarr.Cli.IntegrationTests/BaseCommandSetupIntegrationTest.cs b/tests/Recyclarr.Cli.IntegrationTests/BaseCommandSetupIntegrationTest.cs index 0fffa37b..15f67689 100644 --- a/tests/Recyclarr.Cli.IntegrationTests/BaseCommandSetupIntegrationTest.cs +++ b/tests/Recyclarr.Cli.IntegrationTests/BaseCommandSetupIntegrationTest.cs @@ -54,7 +54,7 @@ internal class BaseCommandSetupIntegrationTest : CliIntegrationFixture var sut = Resolve(); sut.OnFinish(); - maxFiles.Should().BeGreaterThan(0); + maxFiles.Should().BePositive(); Fs.AllFiles.Where(x => x.StartsWith(Paths.LogDirectory.FullName)) .Should().HaveCount(maxFiles); } diff --git a/tests/Recyclarr.Tests/Common/Extensions/DictionaryExtensionsTest.cs b/tests/Recyclarr.Tests/Common/Extensions/DictionaryExtensionsTest.cs index a4887a88..7b7895a1 100644 --- a/tests/Recyclarr.Tests/Common/Extensions/DictionaryExtensionsTest.cs +++ b/tests/Recyclarr.Tests/Common/Extensions/DictionaryExtensionsTest.cs @@ -27,7 +27,7 @@ public class DictionaryExtensionsTest var theValue = dict.GetValueOrDefault(200); - dict.Should().HaveCount(1).And.Contain(100, sample); + dict.Should().ContainSingle().And.Contain(100, sample); theValue.Should().BeNull(); }