question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blazor ErrorBoundary child component exception

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have one custom ErrorBoundary component that I’m placing in MainLayout. If there is an error in any child component, it goes into an endless loop within the parent component. Thank you in advance for your help. I’m also including the link to the solution:

https://github.com/accacc/EndlessLoop

Expected Behavior

no recursive loop

Steps To Reproduce

    @inherits ErrorBoundary
    @inject ISnackbar Snackbar
    @if (CurrentException is null)
    {
        @ChildContent
    }
    else if (ErrorContent is not null)
    {
        @ErrorContent(CurrentException)
    }
    else
    {
        @ChildContent
    
        @foreach (var exception in receivedExceptions)
        {
            Snackbar.Add(@exception.Message, Severity.Error);
            @exception.Message
        }  
    
    }
    
    @code {
        List<Exception> receivedExceptions = new();
    
        protected override Task OnErrorAsync(Exception exception)
        {
            receivedExceptions.Add(exception);
            return  base.OnErrorAsync(exception);
        }   
        
    }

Parent Component

@page "/"

@inject ISnackbar Snackbar
@using System.Net;


<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

@error - @ex

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />

<MyComp></MyComp>


@code{
    public int error;
    public string ex;

    protected override Task OnInitializedAsync()
    {
        //Snackbar.Add("OK", Severity.Error);
        return base.OnInitializedAsync();
    }
}

Child component

<h3>MyComp</h3>

@error

@code {
    public int error;

    protected async override Task OnInitializedAsync()
    {
        await Task.Delay(5);

        error++;
        throw new Exception("aaa");

    }
}

Exceptions (if any)

no

.NET Version

6.0

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 3 months ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveSandersonMScommented, Jun 15, 2023

You’re calling Recover from inside the rendering logic of your error boundary. So each time there’s an exception, it will instantly switch back into a non-error state, and hence re-render its original child content, which will then throw again. That’s why there’s an infinite loop. Also you’re only waiting for 5ms before throwing, so this is throwing 200 times per second.

To avoid the infinite loop, don’t call Recover from your rendering logic. If you’re going to call Recover at all, you should only do so:

  • When the user performs some gesture like clicking a button to say they want to retry
  • Or perhaps if something else has changed in your system to give you a reason to believe the error isn’t going to happen again
0reactions
SteveSandersonMScommented, Jun 19, 2023

Not if your goal is to have “on error continue next”-style semantics, no.

If your goal is to perform logging, consider implementing a custom logger provider (docs: https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/logging?view=aspnetcore-7.0#custom-logger-provider-blazor-webassembly)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor ErrorBoundary child component exception
I have one custom ErrorBoundary component that I'm placing in MainLayout. If there is an error in any child component, it goes into...
Read more >
Blazor WebAssembly Exception Handling With Error ...
The ErrorBoundary component enables us to render child content when there is no error, and subsequently render an error UI when an unhandled ......
Read more >
Blazor App Handling Errors Using Error Boundaries ...
ErrorBoundary can be wrapped around any Blazor component, so it will display content if no error, but if any child component throws an...
Read more >
Unhandled Exceptions in Blazor Server with Error ...
Inspired by error boundaries in React, the errorBoundary component attempts to catch recoverable errors that can't permanently corrupt state—and ...
Read more >
Error Handling in Blazor Server .NET 7
The ErrorContent element is a child element of the ErrorBoundary component in Blazor, and it is used to define the content that will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found