Skip to content

Commit

Permalink
refactor(JSException): remove JSException error log (#5142)
Browse files Browse the repository at this point in the history
* chore: update catch JSException logic

* test: 更新单元测试
  • Loading branch information
ArgoZhang authored Jan 17, 2025
1 parent 809149f commit cf897cd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/BootstrapBlazor/Extensions/JSModuleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ public static async Task<JSModule> LoadModule(this IJSRuntime jsRuntime, string
fileName = $"{fileName}?v={version}";
}

var jSObjectReference = await jsRuntime.InvokeAsync<IJSObjectReference>(identifier: "import", fileName);
IJSObjectReference? jSObjectReference = null;
try
{
jSObjectReference = await jsRuntime.InvokeAsync<IJSObjectReference>(identifier: "import", fileName);
}
catch (JSException)
{
#if DEBUG
throw;
#endif
}
return new JSModule(jSObjectReference);
}

Expand Down
16 changes: 16 additions & 0 deletions test/UnitTest/Extensions/JSModuleExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public async Task LoadModule_Exception()
{
var jsRuntime = new MockJSRuntime();
await Assert.ThrowsAsync<TaskCanceledException>(() => jsRuntime.LoadModule("./mock.js", "test"));

var jsRuntime2 = new JSExceptionJSRuntime();
await Assert.ThrowsAsync<JSException>(() => jsRuntime2.LoadModule("./mock.js", "test"));
}

[Fact]
Expand Down Expand Up @@ -106,4 +109,17 @@ class MockJSRuntime : IJSRuntime
throw new TaskCanceledException();
}
}

class JSExceptionJSRuntime : IJSRuntime
{
public ValueTask<TValue> InvokeAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier, object?[]? args)
{
throw new JSException("test-js-exception");
}

public ValueTask<TValue> InvokeAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier, CancellationToken cancellationToken, object?[]? args)
{
throw new JSException("test-js-exception");
}
}
}

0 comments on commit cf897cd

Please sign in to comment.