@@ -42,6 +42,11 @@ public ComponentBase()
4242 } ;
4343 }
4444
45+ /// <summary>
46+ /// Nearest ErrorBoundaryBase component
47+ /// </summary>
48+ [ CascadingParameter ] internal IErrorBoundary ? ErrorBoundary { get ; set ; }
49+
4550 /// <summary>
4651 /// Gets the <see cref="Components.RendererInfo"/> the component is running on.
4752 /// </summary>
@@ -273,7 +278,23 @@ public virtual Task SetParametersAsync(ParameterView parameters)
273278
274279 private async Task RunInitAndSetParametersAsync ( )
275280 {
276- OnInitialized ( ) ;
281+ try
282+ {
283+ OnInitialized ( ) ;
284+ }
285+ catch ( Exception err )
286+ {
287+ // If we are guarded by ErrorBoundary that should render ChildContent,
288+ // even there was thrown one or more exceptions
289+ if ( ErrorBoundary ? . RenderOnException == true )
290+ {
291+ _ = _renderHandle . DispatchExceptionAsync ( err ) ;
292+ }
293+ else
294+ {
295+ throw ;
296+ }
297+ }
277298 var task = OnInitializedAsync ( ) ;
278299
279300 if ( task . Status != TaskStatus . RanToCompletion && task . Status != TaskStatus . Canceled )
@@ -283,7 +304,7 @@ private async Task RunInitAndSetParametersAsync()
283304 // to defer calling StateHasChanged up until the first bit of async code happens or until
284305 // the end. Additionally, we want to avoid calling StateHasChanged if no
285306 // async work is to be performed.
286- if ( task . Status != TaskStatus . Faulted )
307+ if ( task . Status != TaskStatus . Faulted || ErrorBoundary ? . RenderOnException == true )
287308 {
288309 StateHasChanged ( ) ;
289310 }
@@ -312,7 +333,24 @@ private async Task RunInitAndSetParametersAsync()
312333
313334 private Task CallOnParametersSetAsync ( )
314335 {
315- OnParametersSet ( ) ;
336+ try
337+ {
338+ OnParametersSet ( ) ;
339+ }
340+ catch ( Exception err )
341+ {
342+ // If we are guarded by ErrorBoundary that should render ChildContent,
343+ // even there was thrown one or more exceptions
344+ if ( ErrorBoundary ? . RenderOnException == true )
345+ {
346+ _ = _renderHandle . DispatchExceptionAsync ( err ) ;
347+ }
348+ else
349+ {
350+ throw ;
351+ }
352+ }
353+
316354 var task = OnParametersSetAsync ( ) ;
317355 // If no async work is to be performed, i.e. the task has already ran to completion
318356 // or was canceled by the time we got to inspect it, avoid going async and re-invoking
@@ -322,7 +360,7 @@ private Task CallOnParametersSetAsync()
322360
323361 // We always call StateHasChanged here as we want to trigger a rerender after OnParametersSet and
324362 // the synchronous part of OnParametersSetAsync has run.
325- if ( task . Status != TaskStatus . Faulted )
363+ if ( task . Status != TaskStatus . Faulted || ErrorBoundary ? . RenderOnException == true )
326364 {
327365 StateHasChanged ( ) ;
328366 }
0 commit comments