Blazor ErrorBoundary child component exception
See original GitHub issueIs 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:
- Created 3 months ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 callRecover
at all, you should only do so: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)