|
| 1 | +using System.Text; |
| 2 | + |
| 3 | +namespace Bunit; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Exception used to indicate that an invocation was received by a JSRuntime invocation handler, |
| 7 | +/// but the handler was not configured with a result (via SetResult, SetVoidResult, SetCanceled, or SetException). |
| 8 | +/// This causes the invocation to hang indefinitely. |
| 9 | +/// </summary> |
| 10 | +public sealed class JSRuntimeInvocationNotSetException : Exception |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Gets the invocation that was not handled with a result. |
| 14 | + /// </summary> |
| 15 | + public JSRuntimeInvocation Invocation { get; } |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="JSRuntimeInvocationNotSetException"/> class |
| 19 | + /// with the provided <see cref="Invocation"/> attached. |
| 20 | + /// </summary> |
| 21 | + /// <param name="invocation">The invocation that was not provided with a result.</param> |
| 22 | + public JSRuntimeInvocationNotSetException(JSRuntimeInvocation invocation) |
| 23 | + : base(CreateErrorMessage(invocation)) |
| 24 | + { |
| 25 | + Invocation = invocation; |
| 26 | + } |
| 27 | + |
| 28 | + [SuppressMessage("Minor Code Smell", "S6618:\"string.Create\" should be used instead of \"FormattableString\"", Justification = "string.Create not supported in all TFs")] |
| 29 | + private static string CreateErrorMessage(JSRuntimeInvocation invocation) |
| 30 | + { |
| 31 | + var sb = new StringBuilder(); |
| 32 | + sb.AppendLine("bUnit's JSInterop invocation handler was setup to handle the call:"); |
| 33 | + sb.AppendLine(); |
| 34 | + sb.AppendLine($" {invocation.InvocationMethodName}"); |
| 35 | + sb.AppendLine(); |
| 36 | + sb.AppendLine("However, the invocation handler was not configured to return a result,"); |
| 37 | + sb.AppendLine("causing the invocation to hang indefinitely."); |
| 38 | + sb.AppendLine(); |
| 39 | + sb.AppendLine("To fix this, configure the handler to return a result using one of the following methods:"); |
| 40 | + sb.AppendLine(); |
| 41 | + sb.AppendLine(" handler.SetVoidResult();"); |
| 42 | + sb.AppendLine(" handler.SetResult(result);"); |
| 43 | + sb.AppendLine(" handler.SetCanceled();"); |
| 44 | + sb.AppendLine(" handler.SetException(new Exception(\"error message\"));"); |
| 45 | + |
| 46 | + return sb.ToString(); |
| 47 | + } |
| 48 | +} |
0 commit comments