Skip to content

Commit 016fec7

Browse files
authored
Fix settings migration crash (#348)
* Ensure the new settings directory exists before trying to copy the old settings file into it. * Make settings upgrade exceptions nonfatal
1 parent 20f9200 commit 016fec7

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/FLExBridge/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ static void Main(string[] args)
5252
// exception handler.
5353
}
5454

55-
Settings.UpgradeSettingsIfNecessary(Settings.Default, Application.CompanyName, Application.ProductName);
56-
5755
SetUpErrorHandling();
5856

57+
Settings.UpgradeSettingsIfNecessary(Settings.Default, Application.CompanyName, Application.ProductName);
58+
5959
// Use Gtk3
6060
GraphicsManager.GtkVersionInUse = GraphicsManager.GTK3;
6161

src/LibTriboroughBridge-ChorusPlugin/Properties/Settings.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using SIL.LCModel.Utils;
1010
using SIL.PlatformUtilities;
11+
using SIL.Reporting;
1112

1213
namespace LibTriboroughBridgeChorusPlugin.Properties
1314
{
@@ -25,12 +26,26 @@ public static void UpgradeSettingsIfNecessary<T>(T settings, string companyName,
2526
return;
2627
}
2728

28-
MigrateNonCrossPlatformSettings(companyName, appName);
29+
TryTo(() => MigrateNonCrossPlatformSettings(companyName, appName));
2930

30-
settings.Upgrade();
31+
TryTo(settings.Upgrade);
32+
33+
// Whether or not we succeeded, don't try again.
3134
settings.CallUpgrade = false;
3235
}
3336

37+
private static void TryTo(Action doThis)
38+
{
39+
try
40+
{
41+
doThis();
42+
}
43+
catch (Exception e)
44+
{
45+
ErrorReport.ReportNonFatalExceptionWithMessage(e, "Failed to upgrade settings");
46+
}
47+
}
48+
3449
internal static void MigrateNonCrossPlatformSettings(string companyName, string appName)
3550
{
3651
// Find settings from earlier versions that didn't use CrossPlatformSettingsProvider
@@ -51,6 +66,7 @@ internal static void MigrateNonCrossPlatformSettings(string companyName, string
5166
var newSettingsFilePath = Path.Combine(newSettingsDir, settingsFileName);
5267
if (FileUtils.FileExists(latestOldSettingsFile) && !FileUtils.FileExists(newSettingsFilePath))
5368
{
69+
FileUtils.EnsureDirectoryExists(newSettingsDir);
5470
FileUtils.Copy(latestOldSettingsFile, newSettingsFilePath);
5571
}
5672
}

src/LibTriboroughBridge-ChorusPluginTests/SettingsTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public void MigrateCrossPlatformSettings_MigratesLatest([Values("Url", "StrongNa
6767
var newSettingsFilePath = Path.Combine(NewAppDirPath, prevVer, SettingsFileName);
6868
Assert.That(FileUtils.FileExists(newSettingsFilePath));
6969
Assert.That(_fileOs.ReadAllText(newSettingsFilePath), Is.EqualTo(prevConfigXml));
70+
Assert.That(FileUtils.DirectoryExists(Path.GetDirectoryName(newSettingsFilePath)),
71+
"MockFileOS doesn't always care about directories existing. Real filesystems do.");
7072
}
7173

7274
[Test]

src/RepositoryUtility/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ static void Main()
4949
// exception handler.
5050
}
5151

52+
SetUpErrorHandling();
53+
5254
LibTriboroughBridgeChorusPlugin.Properties.Settings.UpgradeSettingsIfNecessary(Settings.Default,
5355
Application.CompanyName, SettingsProvider.ProductNameForSettings);
5456

55-
SetUpErrorHandling();
56-
5757
// Use Gtk3
5858
GraphicsManager.GtkVersionInUse = GraphicsManager.GTK3;
5959

0 commit comments

Comments
 (0)