Fixed: Error messages when config file is empty or contains invalid characters

Closes #1104
pull/3113/head
Mark McDowall 8 years ago
parent 4f5d79b189
commit ea0982ecae

@ -17,17 +17,18 @@ namespace NzbDrone.Common.Test
public class ConfigFileProviderTest : TestBase<ConfigFileProvider>
{
private string _configFileContents;
private string _configFilePath;
[SetUp]
public void SetUp()
{
WithTempAsAppPath();
var configFile = Mocker.Resolve<IAppFolderInfo>().GetConfigPath();
_configFilePath = Mocker.Resolve<IAppFolderInfo>().GetConfigPath();
_configFileContents = null;
WithMockConfigFile(configFile);
WithMockConfigFile(_configFilePath);
}
protected void WithMockConfigFile(string configFile)
@ -187,5 +188,30 @@ namespace NzbDrone.Common.Test
Subject.SslPort.Should().Be(sslPort);
}
[Test]
public void should_throw_if_config_file_is_empty()
{
Mocker.GetMock<IDiskProvider>()
.Setup(v => v.FileExists(_configFilePath))
.Returns(true);
Assert.Throws<InvalidConfigFileException>(() => Subject.GetValue("key", "value"));
}
[Test]
public void should_throw_if_config_file_contains_only_null_character()
{
_configFileContents = "\0";
Assert.Throws<InvalidConfigFileException>(() => Subject.GetValue("key", "value"));
}
[Test]
public void should_throw_if_config_file_contains_invalid_xml()
{
_configFileContents = "{ \"key\": \"value\" }";
Assert.Throws<InvalidConfigFileException>(() => Subject.GetValue("key", "value"));
}
}
}

@ -348,6 +348,18 @@ namespace NzbDrone.Core.Configuration
{
if (_diskProvider.FileExists(_configFile))
{
var contents = _diskProvider.ReadAllText(_configFile);
if (contents.IsNullOrWhiteSpace())
{
throw new InvalidConfigFileException($"{_configFile} is empty. Please delete the config file and Sonarr will recreate it.");
}
if (contents.All(char.IsControl))
{
throw new InvalidConfigFileException($"{_configFile} is corrupt. Please delete the config file and Sonarr will recreate it.");
}
return XDocument.Parse(_diskProvider.ReadAllText(_configFile));
}
@ -360,7 +372,7 @@ namespace NzbDrone.Core.Configuration
catch (XmlException ex)
{
throw new InvalidConfigFileException(_configFile + " is invalid, please see the http://wiki.sonarr.tv for steps to resolve this issue.", ex);
throw new InvalidConfigFileException($"{_configFile} is corrupt is invalid. Please delete the config file and Sonarr will recreate it.", ex);
}
}

@ -5,6 +5,10 @@ namespace NzbDrone.Core.Configuration
{
public class InvalidConfigFileException : NzbDroneException
{
public InvalidConfigFileException(string message) : base(message)
{
}
public InvalidConfigFileException(string message, Exception innerException) : base(message, innerException)
{
}

Loading…
Cancel
Save