Blazor ErrorContent not catching an error

David Thielen 3,036 Reputation points
2024-09-18T17:40:41.16+00:00

Hi all;

Blazor Interactive Server - in my MainLayout.razor I have:

<ErrorBoundary>
    <ChildContent>
        <!-- lots of nodes -->
    </ChildContent>
    <ErrorContent Context="error">
        <main class="page--error">
            <div class="main-content">
                @{
                    var guid = Guid.NewGuid().ToString();
                    Logger.LogError(error, $"{error.GetType().Name}: {guid}");
                }
                <h1>Unexpected Error (sorry)</h1>
                <p>
                    The program has encountered an unexpected error (yes there are expected errors). This error has been
                    logged and we will look at it.
                </p>
            </div>
        </main>
    </ErrorContent>
</ErrorBoundary>

When it throws an exception handling an event in one of my components, it never gets to the <ErrorContent>. Why not?

And what do I need to do to log the exception? I want to avoid having to try/catch everywhere just to log the exception.

In addition this occurs silently. No error is displayed to the user. I want to put up a message that says something happened, provide them an error number (guid) and ask them to contact support. I can then use that guid to find the issue in the logs to see what happened.

The code throwing the exception is fired off by OnSelectResources:

<SelectOrganizations Data="@_allOrganizations"
                     Visible="@IsSelectResourcesVisible"
                     Click="@OnSelectResources"/>

Which is:

[Parameter]
public EventCallback<IEnumerable<Organization>?> Click { get; set; }

Which is declared as:

<DxButton RenderStyle="ButtonRenderStyle.Primary"
          Text="@Localizer["Select"]"
          Enabled="@(Values?.Count() > 0 && Values?.Count() <= @MaxAllowSelected)"
          Click="OnSelect" />

And calls:

private void OnSelect()
{
    Visible = false;
    _receivedClick = true;
    if (Click.HasDelegate)
        Click.InvokeAsync(Values);
}

Update: As stated above, this is InteractiveServer mode. In App.razor I have:

    <HeadOutlet @rendermode="RenderMode.InteractiveServer" />
</head>
<body>
    <Routes @rendermode="RenderMode.InteractiveServer" />

??? - thanks - dave

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,559 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 64,486 Reputation points
    2024-09-18T20:12:09.7033333+00:00

    I would guess that the async code throwing the error is not running in the correct context, so the caller is not seeing the error and it is ignored. see this thread:

    https://github.com/dotnet/aspnetcore/issues/27716

    until the await syntax, getting errors from threads was difficult. your code is not using await to call Click.InvokeAsync(...) so the error is lost. you might try:

    private async Task OnSelect()
    {
        Visible = false;
        _receivedClick = true;
        if (Click.HasDelegate)
            await Click.InvokeAsync(Values);
    }
    
    You found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.