Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

ErrorGuard

Since 0.1.0-alpha.24
Keeps the rest of a page working when one part of it throws.

Keeps one part of a page working when another throws: what's inside it is replaced by a Danger Alert ("Something went wrong", what happened, and a next step) instead of ending the circuit or the page. Built on Blazor's ErrorBoundary, so ErrorContent, MaximumErrorCount and logging through ILogger work as there.

A card whose service is down

Put ErrorGuard around a part that can fail, such as a card that calls another service. When it throws, the guard shows an alert in its place and the rest of the page keeps working. In a layout, put it around @Body and set ShowDetails from the environment, so only Development shows the exception.

Static

Preview

Revenue

This month
$48,210
Up 12%

Top customers

Code

Usage.razor

razor
@* A dashboard whose customer list comes from a CRM that's down. The guard keeps the failure to that
   card: the revenue beside it and the rest of the page work. On a live page Try again loads the
   card again (and fails again, as the CRM is still down); on a static page Reload loads the page. *@
<Grid Columns="new() { Base = 1, Md = 2 }" Gap="4">
    <Card TitleLevel="2" Title="Revenue">
        <Stat Label="This month" Value="$48,210" Change="12%" Trend="StatTrend.Up" />
    </Card>
    <Card TitleLevel="2" Title="Top customers">
        <ErrorGuard>
            <TopCustomers />
        </ErrorGuard>
    </Card>
</Grid>

TopCustomers.cs

csharp
using Microsoft.AspNetCore.Components;

namespace IzzyBlazor.Site.Examples.ErrorGuard;

/// <summary>
/// The customers who spent most, from a CRM that's down: loading them throws, as a call to a service
/// answering 503 would.
/// </summary>
public sealed class TopCustomers : ComponentBase
{
    protected override async Task OnInitializedAsync()
    {
        await Task.Yield();
        throw new HttpRequestException("The CRM didn't answer: 503 Service Unavailable.");
    }
}

Add this example to a page with the izzy CLI:

bash
izzy add component ErrorGuard --at <file>:<line>

Details

  • On an interactive page the next step is Try again, which renders the content again; on a static page it's a Reload link to the same address (a full load, not an enhanced one).
  • It recovers when the address changes (ResetOnNavigation), so it's safe around @Body in a layout: Blazor's own boundary stays in its error state after the user navigates away.
  • ShowDetails adds the exception in a collapsed section; set it from the environment so only Development shows it.
  • On a static page, ASP.NET Core fails the request when the content a guard removes held a form (dotnet/aspnetcore#70640). AddIzzyBlazor renders the page again with the guard's message in place; this needs the app's UseExceptionHandler (or the developer exception page). A post isn't rendered again, since its handler could run twice: it gets the app's error page.
  • A popup that renders outside it (a Dialog, a Menu's panel) isn't inside it; guard the content of the popup too.

Mode support

What works in each mode, ErrorGuard
FeatureStaticInteractive
Catches what throws while its content renders or loads, and shows an alert in its placeYesYes
Catches what throws in an event handlerNoYes
Try again on interactive pages, Reload on static onesYesYes
Clears when the address changes, so it's safe around @BodyYesYes
The exception's details, when ShowDetails is setYesYes

Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.

Without JavaScript

Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, the page is static: it catches what throws while rendering, and the Reload link works.

Parameters

ErrorGuard parameters
PropTypeDefault
TitleThe title, in place of IzzyText's "Something went wrong".string?
MessageThe message under the title, in place of IzzyText's "This part of the page stopped working."string?
ShowDetailsAdds the exception, message and stack, in a collapsed section.boolfalse
ResetOnNavigationRecovers when the address changes, so a guard around @Body clears on the next page.booltrue
ClassExtra CSS classes on the wrapper of the error view.string?
ChildContentRenderFragment?
ErrorContentRenderFragment<Exception>?
MaximumErrorCountint100
OnErrorRuns with the exception when what's inside throws, after it's logged; for a toast or your own logging.EventCallback<Exception>

Connection lost

Rejoining the server…