refactor: Address a few more SonarLint issues

pull/259/head
Robert Dailey 6 months ago
parent cb56ab5737
commit 4388515614

@ -31,19 +31,14 @@ public class TemplateConfigCreator(
{ {
CopyTemplate(templateFile, settings); CopyTemplate(templateFile, settings);
} }
catch (FileExistsException e)
{
log.Error(e, "Template configuration file could not be saved");
}
catch (FileLoadException) catch (FileLoadException)
{ {
// Do not log here since the origin of this exception is ConfigParser.Load(), which already has // Do not log here since the origin of this exception is ConfigParser.Load(), which already has
// sufficient logging. // sufficient logging.
} }
catch (Exception e) catch (IOException e)
{ {
log.Error(e, "Unable to save configuration template file"); log.Error(e, "Unable to save configuration template file; skipping");
throw;
} }
} }
} }

@ -4,6 +4,7 @@ using Recyclarr.Compatibility;
using Recyclarr.Config.ExceptionTypes; using Recyclarr.Config.ExceptionTypes;
using Recyclarr.Config.Parsing.ErrorHandling; using Recyclarr.Config.Parsing.ErrorHandling;
using Recyclarr.VersionControl; using Recyclarr.VersionControl;
using YamlDotNet.Core;
namespace Recyclarr.Cli.Processors.ErrorHandling; namespace Recyclarr.Cli.Processors.ErrorHandling;
@ -59,6 +60,15 @@ public class ConsoleExceptionHandler(ILogger log, IFlurlHttpExceptionHandler htt
log.Error(e.Message); log.Error(e.Message);
break; 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: default:
return false; return false;
} }

@ -8,7 +8,7 @@ namespace Recyclarr.Cli;
internal static class Program internal static class Program
{ {
public static int Main(string[] args) public static async Task<int> Main(string[] args)
{ {
var builder = new ContainerBuilder(); var builder = new ContainerBuilder();
CompositionRoot.Setup(builder); CompositionRoot.Setup(builder);
@ -43,6 +43,6 @@ internal static class Program
CliSetup.Commands(config); CliSetup.Commands(config);
}); });
return app.Run(args); return await app.RunAsync(args);
} }
} }

@ -2,22 +2,19 @@ using System.IO.Abstractions;
using Recyclarr.Common.Extensions; using Recyclarr.Common.Extensions;
using Recyclarr.Platform; using Recyclarr.Platform;
using Recyclarr.Yaml; using Recyclarr.Yaml;
using Serilog;
using YamlDotNet.Core; using YamlDotNet.Core;
namespace Recyclarr.Settings; namespace Recyclarr.Settings;
public class SettingsProvider : ISettingsProvider public class SettingsProvider : ISettingsProvider
{ {
private readonly ILogger _log;
private readonly IAppPaths _paths; private readonly IAppPaths _paths;
private readonly Lazy<SettingsValues> _settings; private readonly Lazy<SettingsValues> _settings;
public SettingsValues Settings => _settings.Value; public SettingsValues Settings => _settings.Value;
public SettingsProvider(ILogger log, IAppPaths paths, IYamlSerializerFactory serializerFactory) public SettingsProvider(IAppPaths paths, IYamlSerializerFactory serializerFactory)
{ {
_log = log;
_paths = paths; _paths = paths;
_settings = new Lazy<SettingsValues>(() => LoadOrCreateSettingsFile(serializerFactory)); _settings = new Lazy<SettingsValues>(() => LoadOrCreateSettingsFile(serializerFactory));
} }
@ -34,14 +31,7 @@ public class SettingsProvider : ISettingsProvider
} }
catch (YamlException e) catch (YamlException e)
{ {
_log.Error(e, "Exception while parsing settings.yml at line {Line}", e.Start.Line); e.Data["ContextualMessage"] = SettingsContextualMessages.GetContextualErrorFromException(e);
var context = SettingsContextualMessages.GetContextualErrorFromException(e);
if (context is not null)
{
_log.Error(context);
}
throw; throw;
} }
} }

@ -54,7 +54,7 @@ internal class BaseCommandSetupIntegrationTest : CliIntegrationFixture
var sut = Resolve<JanitorCleanupTask>(); var sut = Resolve<JanitorCleanupTask>();
sut.OnFinish(); sut.OnFinish();
maxFiles.Should().BeGreaterThan(0); maxFiles.Should().BePositive();
Fs.AllFiles.Where(x => x.StartsWith(Paths.LogDirectory.FullName)) Fs.AllFiles.Where(x => x.StartsWith(Paths.LogDirectory.FullName))
.Should().HaveCount(maxFiles); .Should().HaveCount(maxFiles);
} }

@ -27,7 +27,7 @@ public class DictionaryExtensionsTest
var theValue = dict.GetValueOrDefault(200); var theValue = dict.GetValueOrDefault(200);
dict.Should().HaveCount(1).And.Contain(100, sample); dict.Should().ContainSingle().And.Contain(100, sample);
theValue.Should().BeNull(); theValue.Should().BeNull();
} }

Loading…
Cancel
Save