Skip to content

Commit fa9ea09

Browse files
committed
Fix reading invalid configuration
1 parent 2a11948 commit fa9ea09

File tree

3 files changed

+86
-10
lines changed

3 files changed

+86
-10
lines changed

WslToolbox.UI/App.xaml.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using WslToolbox.UI.Core.Services;
1919
using WslToolbox.UI.Core.Sinks;
2020
using WslToolbox.UI.Extensions;
21+
using WslToolbox.UI.Helpers;
2122
using WslToolbox.UI.Services;
2223
using WslToolbox.UI.ViewModels;
2324
using WslToolbox.UI.Views.Modals;
@@ -130,7 +131,11 @@ public App()
130131

131132
GetService<AppNotificationService>().Initialize();
132133

134+
#if DEBUG
135+
UnhandledException += App_DebugUnhandledException;
136+
#else
133137
UnhandledException += App_UnhandledException;
138+
#endif
134139
}
135140

136141
private IHost Host { get; }
@@ -172,20 +177,37 @@ private void App_UnhandledException(object sender, UnhandledExceptionEventArgs e
172177
try
173178
{
174179
_logger.LogError(e.Exception, "An UI exception has occurred: {Message}", e.Message);
180+
ShowExceptionDialog(e);
175181
}
176182
catch (Exception)
177183
{
178184
throw new Exception($"Unexpected error {e.Message}");
179185
}
186+
}
180187

181188
#if DEBUG
182-
throw new Exception($"Unexpected error {e.Message}");
183-
#endif
189+
private void App_DebugUnhandledException(object sender, UnhandledExceptionEventArgs e)
190+
{
191+
_logger.LogError(e.Exception, "An UI exception has occurred: {Message}", e.Message);
192+
ShowExceptionDialog(e);
184193
}
194+
#endif
185195

186196
protected override async void OnLaunched(LaunchActivatedEventArgs args)
187197
{
188198
base.OnLaunched(args);
189199
await GetService<IActivationService>().ActivateAsync(args);
190200
}
201+
202+
private void ShowExceptionDialog(UnhandledExceptionEventArgs e)
203+
{
204+
_logger.LogError(e.Exception, "An UI exception has occurred: {Message}", e.Message);
205+
var errorMessage = $"An unhandled exception has occurred and the application must close.{Environment.NewLine}{Environment.NewLine}Log file is written to:{Environment.NewLine}{Toolbox.LogFile}, open log file?";
206+
207+
var messageboxResult = MessageBoxHelper.ShowError(errorMessage);
208+
if (messageboxResult == MessageBoxResult.Yes)
209+
{
210+
ShellHelper.OpenFile(Toolbox.LogFile);
211+
}
212+
}
191213
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace WslToolbox.UI.Helpers;
4+
5+
public static class MessageBoxHelper
6+
{
7+
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
8+
static extern int MessageBox(IntPtr hInstance,
9+
string lpText,
10+
string lpCaption,
11+
uint type
12+
);
13+
14+
public static MessageBoxResult Show(string text, string title, uint type = MessageBoxTypes.MbOk)
15+
{
16+
var messagebox = MessageBox(0, text, title, type);
17+
18+
return (MessageBoxResult) messagebox;
19+
}
20+
21+
public static MessageBoxResult Show(string text, uint type = MessageBoxTypes.MbOk)
22+
{
23+
return Show(text, string.Empty, type);
24+
}
25+
26+
public static MessageBoxResult Show(string text)
27+
{
28+
return Show(text, string.Empty);
29+
}
30+
31+
public static MessageBoxResult ShowError(string text, string title = "")
32+
{
33+
return Show(text, title, MessageBoxTypes.MbIconError + MessageBoxTypes.MbYesNo);
34+
}
35+
}
36+
37+
public static class MessageBoxTypes
38+
{
39+
public const uint MbOk = 0x0;
40+
public const uint MbIconAsterisk = 0x00000040;
41+
public const uint MbYesNo = 0x00000004;
42+
public const uint MbIconError = 0x00000010;
43+
}
44+
45+
public enum MessageBoxResult
46+
{
47+
Unknown = 0,
48+
Ok = 1,
49+
No = 7,
50+
Yes = 6
51+
}

WslToolbox.UI/Services/WslConfigurationService.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,23 @@ public WslConfigModel GetConfig()
3131
var wsl2Section = new Wsl2ConfigSection();
3232
var wsl2SectionConfig = data.Sections.GetSectionData("wsl2")?.Keys.ToDictionary(x => x.KeyName, x => x.Value);
3333

34-
foreach (var wsl2SectionItem in wsl2SectionConfig)
34+
if (wsl2SectionConfig != null)
3535
{
36-
var configKey = wsl2Section.Settings.FirstOrDefault(x => string.Equals(x.Key, wsl2SectionItem.Key, StringComparison.CurrentCultureIgnoreCase));
37-
if (configKey == null)
36+
foreach (var wsl2SectionItem in wsl2SectionConfig)
3837
{
39-
continue;
40-
}
38+
var configKey = wsl2Section.Settings.FirstOrDefault(x => string.Equals(x.Key, wsl2SectionItem.Key, StringComparison.CurrentCultureIgnoreCase));
39+
if (configKey == null)
40+
{
41+
continue;
42+
}
4143

42-
configKey.Value = wsl2SectionItem.Value;
44+
configKey.Value = wsl2SectionItem.Value;
45+
}
4346
}
44-
47+
4548
return new WslConfigModel
4649
{
47-
Wsl2Section = wsl2Section,
50+
Wsl2Section = wsl2Section
4851
};
4952
}
5053

0 commit comments

Comments
 (0)