Skip to content

Commit

Permalink
Revert "Remove error page" (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper authored Nov 9, 2022
1 parent 6b06da5 commit 34cf6e2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
6 changes: 0 additions & 6 deletions src/BlazorBindings.Core/NativeComponentRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;

namespace BlazorBindings.Core
Expand Down Expand Up @@ -121,11 +120,6 @@ protected override Task UpdateDisplayAsync(in RenderBatch renderBatch)
return Task.CompletedTask;
}

protected override void HandleException(Exception exception)
{
ExceptionDispatchInfo.Throw(exception);
}

public void RegisterEvent(ulong eventHandlerId, Action<ulong> unregisterCallback)
{
if (eventHandlerId == 0)
Expand Down
67 changes: 67 additions & 0 deletions src/BlazorBindings.Maui/ErrorPageHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.Graphics;
using System;
using System.Diagnostics;

namespace BlazorBindings.Maui
{
public static class ErrorPageHelper
{
public static void ShowExceptionPage(Exception exception)
{
Debug.WriteLine($"Fatal exception caught: '{exception?.GetType().Name}': '{exception?.Message}'");

Application.Current.Dispatcher.DispatchAsync(() =>
{
Application.Current.MainPage = GetErrorPageForException(exception);
});
}

private static ContentPage GetErrorPageForException(Exception exception)
{
var errorPage = new ContentPage()
{
BackgroundColor = Colors.White,
Title = "Unhandled exception",
Content = new StackLayout
{
Margin = new Thickness(0, top: 30, 0, 0),
Padding = 10,
Children =
{
new Label
{
TextColor = Colors.Black,
FontAttributes = FontAttributes.Bold,
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
Text = "Unhandled exception",
},
new Label
{
TextColor = Colors.Black,
Text = exception?.Message,
},
new ScrollView
{
Content =
new Label
{
TextColor = Colors.Black,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
Text = exception?.StackTrace,
},

},
},
},
};

return errorPage;
}
}
}
5 changes: 5 additions & 0 deletions src/BlazorBindings.Maui/MauiBlazorBindingsRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ async Task<TComponent> AddComponentLocal()
}
}

protected override void HandleException(Exception exception)
{
ErrorPageHelper.ShowExceptionPage(exception);
}

protected override ElementManager CreateNativeControlManager()
{
return new MauiBlazorBindingsElementManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public async Task RenderToExistingControl_MultipleChildren()
}

[Test]
[Ignore("https://github.com/Dreamescaper/BlazorBindings.Maui/issues/42")]
public void ShouldThrowExceptionIfHappenedDuringSyncRender()
{
void action() => _ = _renderer.AddComponent<ComponentWithException>(new NavigationPage());
Expand All @@ -78,10 +79,8 @@ public async Task RendererShouldHandleAsyncExceptions()
var button = (MC.Button)contentView.Content;
button.SendClicked();

await Task.Delay(50);

var exception = _renderer.Exceptions[0];
Assert.That(exception.Message, Is.EqualTo("HandleExceptionTest"));
Assert.That(() => _renderer.Exceptions, Is.Not.Empty.After(1000, 10));
Assert.That(_renderer.Exceptions[0].Message, Is.EqualTo("HandleExceptionTest"));
}

private static MC.Element GetChildContent(MC.Element container)
Expand Down

0 comments on commit 34cf6e2

Please sign in to comment.