Skip to content

Commit d6cbb90

Browse files
committed
fix: Improve error handling
1 parent ce6f0cf commit d6cbb90

File tree

6 files changed

+84
-12
lines changed

6 files changed

+84
-12
lines changed

src/App.xaml.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ public App()
66
{
77
this.InitializeComponent();
88
this.Suspending += OnSuspending;
9+
this.UnhandledException += App_UnhandledException;
910
LoadSettings();
1011
FavoritesHelper.LoadFavoritesOnStartup();
1112
}
1213

14+
private void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
15+
{
16+
NotificationHelper.NotifyUser("An error occured", "An unhandled exception occured, the app my be in an unstable state.\nPlease restart Bluebird as soon as possible.");
17+
e.Handled = true;
18+
}
19+
1320
private void LoadSettings()
1421
{
1522
SearchUrl = SettingsHelper.GetSetting("SearchUrl") ?? "https://www.qwant.com/?q=";

src/Bluebird.csproj

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4-
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
5-
<!-- disable generation of stack trace metadata (needed by Exception.ToString() in NET Native) -->
6-
<DisableStackTraceMetadata>true</DisableStackTraceMetadata>
7-
<!-- disables generation of exception messages in framework-thrown exceptions -->
8-
<DisableExceptionMessages>true</DisableExceptionMessages>
9-
</PropertyGroup>
104
<PropertyGroup>
115
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
126
<LangVersion>12</LangVersion>
@@ -117,6 +111,7 @@
117111
</Compile>
118112
<Compile Include="Core\AppStartupHelper.cs" />
119113
<Compile Include="Core\ModernBlankPage.cs" />
114+
<Compile Include="Core\WebView2CreationError.cs" />
120115
<Compile Include="Core\XAMLTabCreationParams.cs" />
121116
<Compile Include="Core\NewTabHelper.cs" />
122117
<Compile Include="Core\NotificationHelper.cs" />
@@ -162,6 +157,9 @@
162157
<Compile Include="Pages\SplitTabPage.xaml.cs">
163158
<DependentUpon>SplitTabPage.xaml</DependentUpon>
164159
</Compile>
160+
<Compile Include="Pages\WebViewErrorPage.xaml.cs">
161+
<DependentUpon>WebViewErrorPage.xaml</DependentUpon>
162+
</Compile>
165163
<Compile Include="Pages\WebViewPage.xaml.cs">
166164
<DependentUpon>WebViewPage.xaml</DependentUpon>
167165
</Compile>
@@ -299,6 +297,10 @@
299297
<SubType>Designer</SubType>
300298
<Generator>MSBuild:Compile</Generator>
301299
</Page>
300+
<Page Include="Pages\WebViewErrorPage.xaml">
301+
<Generator>MSBuild:Compile</Generator>
302+
<SubType>Designer</SubType>
303+
</Page>
302304
<Page Include="Pages\WebViewPage.xaml">
303305
<Generator>MSBuild:Compile</Generator>
304306
<SubType>Designer</SubType>

src/Core/WebView2CreationError.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Bluebird.Core;
2+
3+
public class WebView2CreationError(string ErrorMsg)
4+
{
5+
public string ErrorMsg { get; private set; } = ErrorMsg;
6+
}

src/Pages/WebViewErrorPage.xaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Page
2+
x:Class="Bluebird.Pages.WebViewErrorPage"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:local="using:Bluebird.Pages"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
mc:Ignorable="d">
9+
10+
<Grid
11+
x:Name="pageChrome"
12+
HorizontalAlignment="Stretch"
13+
VerticalAlignment="Stretch"
14+
Style="{StaticResource PageBorder}">
15+
<ScrollViewer>
16+
<StackPanel x:Name="ErrorList" />
17+
</ScrollViewer>
18+
</Grid>
19+
</Page>

src/Pages/WebViewErrorPage.xaml.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace Bluebird.Pages;
2+
3+
public sealed partial class WebViewErrorPage : Page
4+
{
5+
public WebViewErrorPage()
6+
{
7+
this.InitializeComponent();
8+
}
9+
10+
protected override void OnNavigatedTo(NavigationEventArgs e)
11+
{
12+
base.OnNavigatedTo(e);
13+
14+
string ErrorMsg = string.Empty;
15+
if (e.Parameter != null)
16+
{
17+
var parameters = (WebView2CreationError)e.Parameter;
18+
ErrorMsg = parameters.ErrorMsg;
19+
}
20+
21+
muxc.InfoBar infoBar = new()
22+
{
23+
IsOpen = true,
24+
Severity = muxc.InfoBarSeverity.Error,
25+
Title = "An error occured",
26+
Message = "Read the details below",
27+
IsClosable = false
28+
};
29+
ErrorList.Children.Add(infoBar);
30+
31+
if (!string.IsNullOrEmpty(ErrorMsg))
32+
{
33+
TextBlock ErrorMsgDisplay = new()
34+
{
35+
Text = ErrorMsg,
36+
IsTextSelectionEnabled = true
37+
};
38+
ErrorList.Children.Add(ErrorMsgDisplay);
39+
}
40+
}
41+
}

src/Pages/WebViewPage.xaml.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ private async void WebViewControl_Loaded(object sender, RoutedEventArgs e)
3232
{
3333
await (sender as muxc.WebView2).EnsureCoreWebView2Async();
3434
}
35-
catch
35+
catch (Exception ex)
3636
{
37-
var result = await UI.ShowDialogWithAction("Error", "WebView2 Runtime is not installed which is required to display webpages", "Download WebView2 Runtime", "Close App");
38-
if (result == ContentDialogResult.Primary)
39-
await Launcher.LaunchUriAsync(new Uri("https://go.microsoft.com/fwlink/p/?LinkId=2124703"));
40-
else
41-
CoreApplication.Exit();
37+
WebViewControl?.Close();
38+
Frame.Navigate(typeof(WebViewErrorPage), new WebView2CreationError(ex.StackTrace), new DrillInNavigationTransitionInfo());
4239
}
4340
}
4441

0 commit comments

Comments
 (0)