aspnetcore: ErrorBoundary does not work

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When creating a new Blazor Web App template and using InteractiveServer components without global interactivity… I cannot get the error boundary to show

In a static component with an interactive island, it does not show Error content… in a interactive page, it doesnt show error content either…

Confused what I am doing wrong

Expected Behavior

Error boundary shows child content or error content when an unhandled exception happens in a static page or interactive page/component

Created from Blazor Web app

Steps To Reproduce

Go to the home page, and click the button to increase the count

Exceptions (if any)

warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100] Unhandled exception rendering component: Exception of type 'System.Exception' was thrown. System.Exception: Exception of type 'System.Exception' was thrown. at StaticErrorBoundary.Components.Pages.Counter.IncrementCount() in /Users/garrettlondon/Github/StaticErrorBoundary/Components/Pages/Counter.razor:line 29 at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[T](MulticastDelegate delegate, T arg) at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, Object arg) at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(UInt64 eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs, Boolean waitForQuiescence) fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]

Unhandled exception in circuit 'XOj9HyZGUCrv2y5bZvv5Mswe399H9w8ayWexTB5H--w'. System.Exception: Exception of type 'System.Exception' was thrown. at StaticErrorBoundary.Components.Pages.Counter.IncrementCount() in /Users/garrettlondon/Github/StaticErrorBoundary/Components/Pages/Counter.razor:line 29 at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[T](MulticastDelegate delegate, T arg) at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, Object arg) at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync(UInt64 eventHandlerId, EventFieldInfo fieldInfo, EventArgs eventArgs, Boolean waitForQuiescence)

.NET Version

8.0.302

Anything else?

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 3
  • Comments: 24 (5 by maintainers)

Most upvoted comments

I can confirm @guardrex that we also find it confusing. We are looking for a reliable way to log Exceptions from a standalone Blazor Web Assembly Application to a Server within a Production Environment. Sources indicate that using the OnErrorAsync Method of the ErrorBoundary Component as a “global exception handler” is a way of achieving this. On closer inspection, however, it seems that Exceptions thrown from an HTML OnClick Event are not reliably handled by the ErrorBoundary Component. For other Developers looking to do something similar then it may be helpful for Microsoft to add a Note to this effect to their Documentation to hopefully minimise wasted effort going forwards. Having given this considerable thought we think the best way forwards for us, until Microsoft comes up with a better solution, is to remove the ErrorBoundary Component from our Application. @danroth27 in response to your Question to @garrettlondon1 ideally we would like a Component that provides a reliable way of handling Exceptions globally rather than adding the same code multiple times to Try…Catch blocks, which seems to be the only solution as it stands.

@danroth27 thanks for the quick reply…

ErrorBoundary is definitely the proper solution for Interactive blazor error handling

The problem is that you have to wrap every single interactive component in it because App.razor/Router is now static

  • This is fairly manageable and im fine with it…

All this story is asking for, is to make every interactive action inside of the ErrorBoundary able to bubble up the component tree correctly, and hit the ErrorBoundary properly: not throw an unhandled exception

Can we get this pulled out of the backlog?

@cwoodward-taurus … I’m going to enhance the coverage with additional guidance ☝️. However, I don’t have product unit remarks yet on the main question that you’re asking about outside of just remarking that ErrorBoundary no-ops on them …

Exceptions thrown from an HTML OnClick Event are not reliably handled by the ErrorBoundary Component

Article remarks for that scenario will need to come from the product unit engineers here. I’ll include anything else they want to say about it when I work that issue, and I should reach that issue next week after we get back from the Labor Day holiday.

I don’t agree with your feedback (yet 😄), so I yield to @MackinnonBuck and @danroth27 to determine if further changes are needed. If not, we’ll continue to take feedback and see if others find it confusing.

Most of what you’re discussing is covered prior to that example. For analysis, @MackinnonBuck and @danroth27 should see the full section …

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

I am using blazor interactive components in MVC project and the fact that I have to wrap every component I create in another component just to catch errors is a bit frustrating, to say the least. Or is there another way to globally catch errors in such a case?

@cwoodward-taurus I want to note there is a solution. You need to create another wrapper on top of every interactive component you have…

@IevgeniiKunshchykov example calls it WrappingComponent

Page.razor (static)

  • SafeWrappingComponent.razor
  • View.razor

Its incredibly painful, but it works for all scenarios. Now that I’m thinking… I wonder if you can make an interactive component that wraps interactive ChildContent.

Almost, wrapping ErrorBoundary itself inside of another interactive component which takes a ChildContent parameter. Will try it out.

  • Nope, Interactive components cant be serialized as ChildContent in static pages, even if its wrapped in another static component

Hello guys,

I’ve got two examples:

  1. This example contains case when error is caught. There are 2 components: 1st rises error and seconds wraps 1st with ErrorBoundary

ComponentWithError.razor image

WrappingComponent.razor image

  1. This example contains case when error is not caugh and unhandled exception happens. image

I found the reson why errorboundary does not work when error happens just inside content but not in child component There is such a class .Microsoft.AspNetCore.Components.RenderTree.Renderer.cs which contains HandleExceptionViaErrorBoundary method that recursively tries to find ErrorBoundary to handle exception. But search goes in tree by components. image

That’s why my first case works. Because WrappingComponent.razor contains ErrorBoundary But second case has no wrapping component or that component just does not have ErrorBoundary . ErrorBoundary is located just inside that component. And that case is not covered by that loop in Renderer.cs

Seems like we are obliged always to wrap components with ErrorBoundary but cannot just wrap some content.

Any reason for that ??? Why whole tree cannot be checked ??