diff --git a/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs b/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs index 76383d88e..31daee47d 100644 --- a/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs +++ b/src/NzbDrone.Common.Test/ConfigFileProviderTest.cs @@ -17,17 +17,18 @@ namespace NzbDrone.Common.Test public class ConfigFileProviderTest : TestBase { private string _configFileContents; + private string _configFilePath; [SetUp] public void SetUp() { WithTempAsAppPath(); - var configFile = Mocker.Resolve().GetConfigPath(); + _configFilePath = Mocker.Resolve().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() + .Setup(v => v.FileExists(_configFilePath)) + .Returns(true); + + Assert.Throws(() => Subject.GetValue("key", "value")); + } + + [Test] + public void should_throw_if_config_file_contains_only_null_character() + { + _configFileContents = "\0"; + + Assert.Throws(() => Subject.GetValue("key", "value")); + } + + [Test] + public void should_throw_if_config_file_contains_invalid_xml() + { + _configFileContents = "{ \"key\": \"value\" }"; + + Assert.Throws(() => Subject.GetValue("key", "value")); + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs index b9c509e64..7db6d8508 100644 --- a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -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); } } diff --git a/src/NzbDrone.Core/Configuration/InvalidConfigFileException.cs b/src/NzbDrone.Core/Configuration/InvalidConfigFileException.cs index 7069749ae..1251cf30a 100644 --- a/src/NzbDrone.Core/Configuration/InvalidConfigFileException.cs +++ b/src/NzbDrone.Core/Configuration/InvalidConfigFileException.cs @@ -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) { }