Add support for error boundaries in Blazor
captainsafia opened this issue · 6 comments
Summary
This proposes adding an error boundaries feature to Blazor, as inspired by the implementation in React. This feature would allow developers to implement logic on individual components to capture any excpetions, log, display a fallback UI, send telemetry, etc.
This feature was proposed in #13452 but I'm pulling it out into a separate issue to be more precise about the proposed solution.
Motivation
A meaningfully complex Blazor app will contain numerous components, both first-party and third-party. There's no way to guarantee that all exception cases will be handled in a component. There's also no way to indicate that even though an unhandled exception occurred in one part of the app, that the entire app is not compromised.
Goals
- Allow users to provide fallback UIs for select component subtrees in the event that there is an exception there
- Allow users to build apps with cleaner fallback experiences in the event of unexpected UIs
- Give users more fine-grained control over how failures are handled in the app
Non-goals
- Implement any kind of global exception handling in Blazor (we already have
ILoggerfor logging, and beyond that, truly unhandled exceptions mean the app is in an undefined state which is unsafe to continue) - Add more sophisticated exception handling to the components in Blazor (besides the APIs needed to build this feature)
Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.
This would be wonderful. At a motivating use case, here’s a situation I can’t currently handle in Blazor:
<div>
<MyComponent />
<ThirdPartyComponent Attr=“null” />
</div>where ThirdPartyComponent.razor contains something like this:
@code {
[Parameter] public string Attr { get; set; }
}
<p>@Attr.ToLower()</p>Right now, there’s no way to salvage the render tree and display at least MyComponent, due to the failure to handle errors inside the other Component
Just adding my $0.02. I would also love to see Blazor have something similar to React's Error Boundaries. From my perspective, it is difficult to consider Blazor as being production ready until it has this type of a feature. There is no way that a single small unhandled exception in a developer's code, or worse - a third-party component's code, should cripple an entire application unless a developer wishes for that to happen. When an MVC app or WebForms app has an isolated exception, it does not kill the application. But Blazor? Game over. If using Blazor Server, the user's SignalR connection is nuked and state is lost. Not very fun being a developer and having to explain this to management and end users. If Blazor had something like a React Error Boundary, only the faulty component could be disabled showing a user-friendly error message and the rest of the application could continue to function as expected.
I see this issue currently has a Priority Level of 2. Personally, I would make it even higher priority. Application stability is a fundamental requirement.
@SteveSandersonMS Are you thinking that the "global" aspect of #13452 could be handled by the user adding the solution in this issue to a sufficiently high component in the hierarchy, e.g. App.razor?
Yes, if you find it useful to put in error handling logic there.
Done in #30874