1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 02:22:31 +01:00

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

Closes #1104
This commit is contained in:
Mark McDowall 2016-05-21 00:22:12 -07:00
parent 4f5d79b189
commit ea0982ecae
3 changed files with 45 additions and 3 deletions

View File

@ -17,17 +17,18 @@ namespace NzbDrone.Common.Test
public class ConfigFileProviderTest : TestBase<ConfigFileProvider> public class ConfigFileProviderTest : TestBase<ConfigFileProvider>
{ {
private string _configFileContents; private string _configFileContents;
private string _configFilePath;
[SetUp] [SetUp]
public void SetUp() public void SetUp()
{ {
WithTempAsAppPath(); WithTempAsAppPath();
var configFile = Mocker.Resolve<IAppFolderInfo>().GetConfigPath(); _configFilePath = Mocker.Resolve<IAppFolderInfo>().GetConfigPath();
_configFileContents = null; _configFileContents = null;
WithMockConfigFile(configFile); WithMockConfigFile(_configFilePath);
} }
protected void WithMockConfigFile(string configFile) protected void WithMockConfigFile(string configFile)
@ -187,5 +188,30 @@ public void SaveDictionary_should_only_save_specified_values()
Subject.SslPort.Should().Be(sslPort); 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"));
}
} }
} }

View File

@ -348,6 +348,18 @@ private XDocument LoadConfigFile()
{ {
if (_diskProvider.FileExists(_configFile)) 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)); return XDocument.Parse(_diskProvider.ReadAllText(_configFile));
} }
@ -360,7 +372,7 @@ private XDocument LoadConfigFile()
catch (XmlException ex) 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);
} }
} }

View File

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