Skip to content

Commit

Permalink
Code(WEB::ErrorHandling): Implement global error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutak1337 committed May 1, 2024
1 parent bdeb60f commit 1c8f688
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/Client/StellarChat.Client.Web/Layout/CustomErrorBoundary.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 22 additions & 1 deletion src/Client/StellarChat.Client.Web/Layout/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@inherits LayoutComponentBase

@inject IJSRuntime JSRuntime
@inject ISnackbar Snackbar

<MudThemeProvider IsDarkMode="_IsDarkModeEnabled" />
<MudDialogProvider />
Expand All @@ -15,7 +16,17 @@

<Drawer IsDrawerOpen="@_isDrawerOpen" />
<MudMainContent>
@Body
<CustomErrorBoundary>
<ChildContent>
@Body
</ChildContent>
<ErrorContent Context="errorDetails">
@{
ShowErrorSnackbar(@errorDetails.Message);
@Body
}
</ErrorContent>
</CustomErrorBoundary>
</MudMainContent>
</MudLayout>

Expand Down Expand Up @@ -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;
});
}
}

0 comments on commit 1c8f688

Please sign in to comment.