|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.ExceptionServices; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.AspNetCore.Components.Rendering; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.Components |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// A base class for error boundary components. |
| 13 | + /// </summary> |
| 14 | + public abstract class ErrorBoundaryBase : IComponent |
| 15 | + { |
| 16 | + // This deliberately doesn't inherit from ComponentBase because it's not intended to be |
| 17 | + // subclassable using a .razor file. ErrorBoundaryBase shouldn't be used as a base class |
| 18 | + // for all components indiscriminately, because that will lead to undesirable error-ignoring |
| 19 | + // patterns. We expect that subclassing ErrorBoundaryBase to be done mainly by platform |
| 20 | + // authors, providing just a default error UI for their rendering technology and not |
| 21 | + // customizing other aspects of the semantics, such as whether to re-render after an error. |
| 22 | + |
| 23 | + private RenderHandle _renderHandle; |
| 24 | + private Exception? _currentException; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// The content to be displayed when there is no error. |
| 28 | + /// </summary> |
| 29 | + [Parameter] public RenderFragment? ChildContent { get; set; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// The content to be displayed when there is an error. |
| 33 | + /// </summary> |
| 34 | + [Parameter] public RenderFragment<Exception>? ErrorContent { get; set; } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Specifies whether to reset the error state each time this component instance is rendered |
| 38 | + /// by its parent. This allows the child content to be recreated in an attempt to recover from the error. |
| 39 | + /// </summary> |
| 40 | + [Parameter] public bool AutoReset { get; set; } |
| 41 | + |
| 42 | + /// <inheritdoc /> |
| 43 | + public void Attach(RenderHandle renderHandle) |
| 44 | + { |
| 45 | + _renderHandle = renderHandle; |
| 46 | + } |
| 47 | + |
| 48 | + /// <inheritdoc /> |
| 49 | + public Task SetParametersAsync(ParameterView parameters) |
| 50 | + { |
| 51 | + foreach (var parameter in parameters) |
| 52 | + { |
| 53 | + if (parameter.Name.Equals(nameof(ChildContent), StringComparison.OrdinalIgnoreCase)) |
| 54 | + { |
| 55 | + ChildContent = (RenderFragment)parameter.Value; |
| 56 | + } |
| 57 | + else if (parameter.Name.Equals(nameof(ErrorContent), StringComparison.OrdinalIgnoreCase)) |
| 58 | + { |
| 59 | + ErrorContent = (RenderFragment<Exception>)parameter.Value; |
| 60 | + } |
| 61 | + else if (parameter.Name.Equals(nameof(AutoReset), StringComparison.OrdinalIgnoreCase)) |
| 62 | + { |
| 63 | + AutoReset = (bool)parameter.Value; |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + throw new ArgumentException($"The component '{GetType().FullName}' does not accept a parameter with the name '{parameter.Name}'."); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if (AutoReset) |
| 72 | + { |
| 73 | + _currentException = null; |
| 74 | + } |
| 75 | + |
| 76 | + _renderHandle.Render(Render); |
| 77 | + return Task.CompletedTask; |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Logs the exception. |
| 82 | + /// </summary> |
| 83 | + /// <param name="exception">The <see cref="Exception"/> being handled.</param> |
| 84 | + protected abstract ValueTask LogExceptionAsync(Exception exception); |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Renders the default error content. This will only be used when <see cref="ErrorContent"/> |
| 88 | + /// was not supplied. |
| 89 | + /// </summary> |
| 90 | + /// <param name="builder">The <see cref="RenderTreeBuilder"/></param> |
| 91 | + /// <param name="exception">The current exception.</param> |
| 92 | + protected abstract void RenderDefaultErrorContent(RenderTreeBuilder builder, Exception exception); |
| 93 | + |
| 94 | + internal void HandleException(Exception exception) |
| 95 | + { |
| 96 | + if (_currentException is not null) |
| 97 | + { |
| 98 | + // If there's an error while we're already displaying error content, then it's the |
| 99 | + // error content that's failing. Avoid the risk of an infinite error rendering loop. |
| 100 | + ExceptionDispatchInfo.Capture(exception).Throw(); |
| 101 | + } |
| 102 | + |
| 103 | + // TODO: If there's an async exception here, do something with it (can be fatal) |
| 104 | + _ = LogExceptionAsync(exception); |
| 105 | + |
| 106 | + _currentException = exception; |
| 107 | + _renderHandle.Render(Render); |
| 108 | + } |
| 109 | + |
| 110 | + private void Render(RenderTreeBuilder builder) |
| 111 | + { |
| 112 | + if (_currentException is null) |
| 113 | + { |
| 114 | + builder.AddContent(0, ChildContent); |
| 115 | + } |
| 116 | + else if (ErrorContent is not null) |
| 117 | + { |
| 118 | + builder.AddContent(1, ErrorContent(_currentException)); |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + // Even if the subclass tries to re-render the same ChildContent as its default error content, |
| 123 | + // we still won't reuse the subtree components because they are in a different region. So we |
| 124 | + // can be sure the old tree will be correctly disposed. |
| 125 | + builder.OpenRegion(2); |
| 126 | + RenderDefaultErrorContent(builder, _currentException); |
| 127 | + builder.CloseRegion(); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments