Skip to content

ErrorBoundary subclassing, exception context, and recovery #33492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
guardrex opened this issue Aug 30, 2024 · 0 comments · Fixed by #33585
Closed

ErrorBoundary subclassing, exception context, and recovery #33492

guardrex opened this issue Aug 30, 2024 · 0 comments · Fixed by #33585

Comments

@guardrex
Copy link
Collaborator

guardrex commented Aug 30, 2024

Description

Cover ...

  • DOM events aren't captured by ErrorBoundary.
  • The exception context (@context).
  • Recovery with a clear button (component reference with Recover).
  • Custom context (Context={CONTEXT}).
  • Recovery with exception processing (subclassed with overridden OnErrorAsync).

@context with recovery example ...

<ErrorBoundary @ref="errorBoundary">
    <ChildContent>
        <EmbeddedCounter />
    </ChildContent>
    <ErrorContent>
        <div class="alert alert-danger" role="alert">
            <p class="fs-3 fw-bold">😈 A rotten gremlin got us. Sorry!</p>
            <p>@context.GetType(): @context.Message</p>
            <button class="btn btn-info" @onclick="_ => errorBoundary.Recover()">Clear</button>
        </div>
    </ErrorContent>
</ErrorBoundary>

@code {
    private ErrorBoundary? errorBoundary;
}

Custom context ...

<ErrorContent Context="exception">
    @exception.Message
</ErrorContent>

Subclassed CustomErrorBoundary component (CustomErrorBoundary.razor) for custom processing with OnErrorAsync ...

@inherits ErrorBoundary
@inject ILogger<CustomErrorBoundary>? Logger

@if (CurrentException is null)
{
    @ChildContent
}
else if (ErrorContent is not null)
{
    @ErrorContent(CurrentException)
}

@code {
    protected override async Task OnErrorAsync(Exception exception)
    {
        await Task.Yield();
        Logger?.LogError("{Type}: {Message}", exception.GetType(), exception.Message);
    }
}

OR ... preceding component implemented in code ...

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;

namespace BlazorSample;

public class CustomErrorBoundary : ErrorBoundary
{
    [Inject]
    ILogger<CustomErrorBoundary>? Logger {  get; set; }

    protected override async Task OnErrorAsync(Exception exception)
    {
        await Task.Yield();
        Logger?.LogError("{Type}: {Message}", exception.GetType(), exception.Message);
    }
}

Used as ...

<CustomErrorBoundary>
    ...
</CustomErrorBoundary>

Cross-references:

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/handle-errors?view=aspnetcore-8.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/fundamentals/handle-errors.md

Document ID

66a0b1c2-c45c-98ca-9808-6f340a861c44

Article author

@guardrex

@guardrex guardrex added Source - Docs.ms Docs Customer feedback via GitHub Issue ⌚ Not Triaged labels Aug 30, 2024
@github-project-automation github-project-automation bot moved this to Triage in Blazor.Docs Aug 30, 2024
@guardrex guardrex added Pri1 doc-enhancement and removed Source - Docs.ms Docs Customer feedback via GitHub Issue labels Aug 30, 2024
@guardrex guardrex moved this from Triage to In progress in Blazor.Docs Aug 30, 2024
@guardrex guardrex changed the title OnErrorAsync and DOM events not captured for ErrorBoundary ErrorBoundary subclassing Aug 30, 2024
@guardrex guardrex changed the title ErrorBoundary subclassing ErrorBoundary subclassing, exception context, and recovery Aug 30, 2024
@dotnet dotnet deleted a comment from github-actions bot Aug 30, 2024
@guardrex guardrex moved this from In progress to P0/P1 - High Priority in Blazor.Docs Sep 5, 2024
@guardrex guardrex moved this from P0/P1 - High Priority to In progress in Blazor.Docs Sep 11, 2024
@github-project-automation github-project-automation bot moved this from In progress to Done in Blazor.Docs Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants