From 1c8f6880b050876b206e64962260f82a129181e9 Mon Sep 17 00:00:00 2001 From: ktutak1337 Date: Wed, 1 May 2024 20:36:26 +0200 Subject: [PATCH] Code(WEB::ErrorHandling): Implement global error handling --- .../Layout/CustomErrorBoundary.cs | 21 +++++++++++++++++ .../Layout/MainLayout.razor | 23 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/Client/StellarChat.Client.Web/Layout/CustomErrorBoundary.cs diff --git a/src/Client/StellarChat.Client.Web/Layout/CustomErrorBoundary.cs b/src/Client/StellarChat.Client.Web/Layout/CustomErrorBoundary.cs new file mode 100644 index 0000000..76b038c --- /dev/null +++ b/src/Client/StellarChat.Client.Web/Layout/CustomErrorBoundary.cs @@ -0,0 +1,21 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Web; +using Microsoft.AspNetCore.Components.WebAssembly.Hosting; + +namespace StellarChat.Client.Web.Layout; + +public class CustomErrorBoundary : ErrorBoundary +{ + [Inject] + private IWebAssemblyHostEnvironment? Environment { get; set; } + + protected override Task OnErrorAsync(Exception exception) + { + if (Environment!.IsDevelopment()) + { + return base.OnErrorAsync(exception); + } + + return Task.CompletedTask; + } +} diff --git a/src/Client/StellarChat.Client.Web/Layout/MainLayout.razor b/src/Client/StellarChat.Client.Web/Layout/MainLayout.razor index 8133739..116e990 100644 --- a/src/Client/StellarChat.Client.Web/Layout/MainLayout.razor +++ b/src/Client/StellarChat.Client.Web/Layout/MainLayout.razor @@ -1,6 +1,7 @@ @inherits LayoutComponentBase @inject IJSRuntime JSRuntime +@inject ISnackbar Snackbar @@ -15,7 +16,17 @@ - @Body + + + @Body + + + @{ + ShowErrorSnackbar(@errorDetails.Message); + @Body + } + + @@ -44,4 +55,14 @@ } private void ToggleDrawer() => _isDrawerOpen = !_isDrawerOpen; + + private void ShowErrorSnackbar(string message) + { + Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopRight; + + Snackbar.Add(message, Severity.Error, options => + { + options.HideTransitionDuration = 100; + }); + } }