Skip to content

Commit 8530b27

Browse files
committed
Added exception handling to make any missing config settings really obvious
1 parent 9dd6f91 commit 8530b27

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

AzureWebFarm.OctopusDeploy.Tests/AzureWebFarm.OctopusDeploy.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
</ItemGroup>
9292
<ItemGroup>
9393
<Compile Include="Infrastructure\AzureEnvironmentTests.cs" />
94+
<Compile Include="Infrastructure\ConfigSettingsTests.cs" />
9495
<Compile Include="Infrastructure\OctopusDeployTests.cs" />
9596
<Compile Include="Infrastructure\RegistryEditorTests.cs" />
9697
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using AzureWebFarm.OctopusDeploy.Infrastructure;
3+
using Shouldly;
4+
using Xunit;
5+
6+
namespace AzureWebFarm.OctopusDeploy.Tests.Infrastructure
7+
{
8+
public class ConfigSettingsTests
9+
{
10+
[Fact]
11+
public void GivenExceptionWhenGettingConfigSetting_WhenConstructingConfigSettings_ThenThrowNiceException()
12+
{
13+
var e = new Exception("Error");
14+
Func<string, string> getter = _ => { throw e; };
15+
16+
var ex = Assert.Throws<UnableToGetConfigSettingException>(() => new ConfigSettings(getter, getter));
17+
18+
ex.Message.ShouldBe("Unable to get config setting: OctopusServer");
19+
ex.InnerException.ShouldBe(e);
20+
}
21+
}
22+
}

AzureWebFarm.OctopusDeploy/Infrastructure/ConfigSettings.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ internal class ConfigSettings : IConfigSettings
3535

3636
public ConfigSettings(Func<string, string> configSettingsGetter, Func<string, string> configPathGetter)
3737
{
38-
_octopusServer = configSettingsGetter(OctopusServerConfigName);
39-
_octopusApiKey = configSettingsGetter(OctopusApiKeyConfigName);
40-
_tentacleEnvironment = configSettingsGetter(TentacleEnvironmentConfigName);
41-
_tentacleRole = configSettingsGetter(TentacleRoleConfigName);
42-
_tentacleMachineNameSuffix = configSettingsGetter(TentacleMachineNameSuffixConfigName);
43-
44-
_tentacleDeploymentsPath = configPathGetter(TentacleDeploymentsPathConfigName);
45-
_tentacleInstallPath = configPathGetter(TentacleInstallPathConfigName);
38+
_octopusServer = SafeGetConfigSetting(configSettingsGetter, OctopusServerConfigName);
39+
_octopusApiKey = SafeGetConfigSetting(configSettingsGetter, OctopusApiKeyConfigName);
40+
_tentacleEnvironment = SafeGetConfigSetting(configSettingsGetter, TentacleEnvironmentConfigName);
41+
_tentacleRole = SafeGetConfigSetting(configSettingsGetter, TentacleRoleConfigName);
42+
_tentacleMachineNameSuffix = SafeGetConfigSetting(configSettingsGetter, TentacleMachineNameSuffixConfigName);
43+
44+
_tentacleDeploymentsPath = SafeGetConfigSetting(configPathGetter, TentacleDeploymentsPathConfigName);
45+
_tentacleInstallPath = SafeGetConfigSetting(configPathGetter, TentacleInstallPathConfigName);
4646
}
4747

4848
public string OctopusServer { get { return _octopusServer; } }
@@ -58,5 +58,31 @@ public bool IsConfigSettingName(string name)
5858
{
5959
return ConfigSettingsNames.Contains(name);
6060
}
61+
62+
public string SafeGetConfigSetting(Func<string, string> configSettingsGetter, string settingName)
63+
{
64+
try
65+
{
66+
return configSettingsGetter(settingName) ?? "";
67+
}
68+
catch (Exception e)
69+
{
70+
throw new UnableToGetConfigSettingException(settingName, e);
71+
}
72+
}
73+
}
74+
75+
/// <summary>
76+
/// Error getting a config setting.
77+
/// </summary>
78+
public class UnableToGetConfigSettingException : Exception
79+
{
80+
/// <summary>
81+
/// Create the exception for the setting in question.
82+
/// </summary>
83+
/// <param name="settingName">The name of the setting an error was raised for</param>
84+
/// <param name="exception">The exception that was raised when getting the setting</param>
85+
public UnableToGetConfigSettingException(string settingName, Exception exception)
86+
: base("Unable to get config setting: " + settingName, exception) {}
6187
}
6288
}

0 commit comments

Comments
 (0)