ErrorGuard
Since 0.1.0-alpha.24Keeps 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
Top customers
Code
Usage.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
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:
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. ShowDetailsadds 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
| Feature | Static | Interactive |
|---|---|---|
| Catches what throws while its content renders or loads, and shows an alert in its place | Yes | Yes |
| Catches what throws in an event handler | No | Yes |
| Try again on interactive pages, Reload on static ones | Yes | Yes |
| Clears when the address changes, so it's safe around @Body | Yes | Yes |
| The exception's details, when ShowDetails is set | Yes | Yes |
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
| Prop | Type | Default |
|---|---|---|
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. | bool | false |
ResetOnNavigationRecovers when the address changes, so a guard around @Body clears on the next page. | bool | true |
ClassExtra CSS classes on the wrapper of the error view. | string? | |
ChildContent | RenderFragment? | |
ErrorContent | RenderFragment<Exception>? | |
MaximumErrorCount | int | 100 |
OnErrorRuns with the exception when what's inside throws, after it's logged; for a toast or your own logging. | EventCallback<Exception> |