Skip to content

Commit 6b968b1

Browse files
committed
Move error handling to App.xaml.cs
1 parent 01b623d commit 6b968b1

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

src/CodeSnip/App.xaml.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@ protected override async void OnStartup(StartupEventArgs e)
1111
var splashScreen = new Views.SplashScreenView.SplashScreen();
1212
splashScreen.Show();
1313

14-
var mainWindow = new MainWindow();
15-
await PerformInitializationAsync(mainWindow);
16-
MainWindow = mainWindow;
17-
mainWindow.Show();
18-
19-
splashScreen.Close();
14+
try
15+
{
16+
var mainWindow = new MainWindow();
17+
await PerformInitializationAsync(mainWindow);
18+
MainWindow = mainWindow;
19+
mainWindow.Show();
20+
}
21+
catch (Exception ex)
22+
{
23+
MessageBox.Show($"A critical error occurred during application startup and the application will now close.\n\nError: {ex.Message}", "Startup Error", MessageBoxButton.OK, MessageBoxImage.Error);
24+
Shutdown();
25+
}
26+
finally
27+
{
28+
splashScreen.Close();
29+
}
2030
}
2131
private async Task PerformInitializationAsync(MainWindow mainWindow)
2232
{

src/CodeSnip/MainViewModel.cs

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -110,43 +110,32 @@ public enum SnippetFilterMode
110110
public MainViewModel(IFlyoutService flyoutService)
111111
{
112112
_flyoutService = flyoutService;
113-
try
114-
{
115-
_menuOpenIcon = Application.Current.Resources["MenuOpen"] as Geometry;
116-
_menuCloseIcon = Application.Current.Resources["MenuClose"] as Geometry;
117-
if (_menuOpenIcon == null || _menuCloseIcon == null)
118-
{
119-
throw new InvalidOperationException("Icons not found in resources.");
120-
}
121-
122-
SplitViewOpenPaneLength = settingsService.PanelLength;
123-
WindowX = settingsService.WindowX;
124-
WindowY = settingsService.WindowY;
125-
WindowWidth = settingsService.WindowWidth;
126-
WindowHeight = settingsService.WindowHeight;
127-
opt.EnableEmailHyperlinks = settingsService.EnableEmailLinks;
128-
opt.EnableHyperlinks = settingsService.EnableHyperinks;
129-
opt.ConvertTabsToSpaces = settingsService.TabToSpaces;
130-
opt.HighlightCurrentLine = settingsService.HighlightLine;
131-
opt.IndentationSize = settingsService.IntendationSize;
132-
133-
IsFilteringEnabled = settingsService.EnableFiltering;
134-
EnableBraceStyleFolding = settingsService.EnableBraceStyleFolding;
135-
EnablePythonFolding = settingsService.EnablePythonFolding;
136-
EnableXmlFolding = settingsService.EnableXmlFolding;
137-
ShowEmptyLanguages = settingsService.ShowEmptyLanguages;
138-
ShowEmptyCategories = settingsService.ShowEmptyCategories;
139113

140-
}
141-
catch (InvalidOperationException ex)
142-
{
143-
MessageBox.Show($"Error while starting the application:\n{ex.Message}");
144-
}
145-
catch (Exception ex)
114+
_menuOpenIcon = Application.Current.Resources["MenuOpen"] as Geometry;
115+
_menuCloseIcon = Application.Current.Resources["MenuClose"] as Geometry;
116+
if (_menuOpenIcon == null || _menuCloseIcon == null)
146117
{
147-
MessageBox.Show($"Unexpected error:\n{ex.Message}");
118+
throw new InvalidOperationException("Icons not found in resources.");
148119
}
149120

121+
SplitViewOpenPaneLength = settingsService.PanelLength;
122+
WindowX = settingsService.WindowX;
123+
WindowY = settingsService.WindowY;
124+
WindowWidth = settingsService.WindowWidth;
125+
WindowHeight = settingsService.WindowHeight;
126+
opt.EnableEmailHyperlinks = settingsService.EnableEmailLinks;
127+
opt.EnableHyperlinks = settingsService.EnableHyperinks;
128+
opt.ConvertTabsToSpaces = settingsService.TabToSpaces;
129+
opt.HighlightCurrentLine = settingsService.HighlightLine;
130+
opt.IndentationSize = settingsService.IntendationSize;
131+
132+
IsFilteringEnabled = settingsService.EnableFiltering;
133+
EnableBraceStyleFolding = settingsService.EnableBraceStyleFolding;
134+
EnablePythonFolding = settingsService.EnablePythonFolding;
135+
EnableXmlFolding = settingsService.EnableXmlFolding;
136+
ShowEmptyLanguages = settingsService.ShowEmptyLanguages;
137+
ShowEmptyCategories = settingsService.ShowEmptyCategories;
138+
150139
}
151140
public async Task InitializeAsync()
152141
{

0 commit comments

Comments
 (0)