-
Notifications
You must be signed in to change notification settings - Fork 1.9k
HybridWebView Bi-Directional Exception Handling #31521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PureWeen
merged 6 commits into
net10.0
from
copilot/fix-cc3b23cd-67af-45d5-96c5-200f2371b602
Sep 12, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ad0324f
Initial plan
Copilot 4d79ff9
Implement HybridWebView C# exception bubbling to JavaScript
Copilot c9402b7
Add demo exception handling to HybridWebView sample
Copilot dcc70d3
Add JavaScript exception handling tests for HybridWebView
Copilot b5cd385
Handle reflection exceptions
mattleibow 7fb8324
AI was right!
mattleibow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests_ExceptionHandling.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| #nullable enable | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Maui.DeviceTests; | ||
|
|
||
| [Category(TestCategory.HybridWebView)] | ||
| #if WINDOWS | ||
| [Collection(WebViewsCollection)] | ||
| #endif | ||
| public partial class HybridWebViewTests_ExceptionHandling : HybridWebViewTestsBase | ||
mattleibow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| [Fact] | ||
| public Task CSharpMethodThatThrowsException_ShouldPropagateToJavaScript() => | ||
| RunTest("exception-tests.html", async (hybridWebView) => | ||
| { | ||
| var invokeJavaScriptTarget = new TestExceptionMethods(); | ||
| hybridWebView.SetInvokeJavaScriptTarget(invokeJavaScriptTarget); | ||
|
|
||
| // Tell JavaScript to invoke the method that throws an exception | ||
| hybridWebView.SendRawMessage("ThrowException"); | ||
|
|
||
| // Wait for JavaScript to handle the exception | ||
| await WebViewHelpers.WaitForHtmlStatusSet(hybridWebView); | ||
|
|
||
| // Check that JavaScript caught the exception | ||
| var caughtError = await hybridWebView.EvaluateJavaScriptAsync("GetLastError()"); | ||
| Assert.Equal("Test exception message", caughtError); | ||
|
|
||
| // Check that the method was called before throwing | ||
| Assert.Equal("ThrowException", invokeJavaScriptTarget.LastMethodCalled); | ||
| }); | ||
|
|
||
| [Fact] | ||
| public Task CSharpAsyncMethodThatThrowsException_ShouldPropagateToJavaScript() => | ||
| RunTest("exception-tests.html", async (hybridWebView) => | ||
| { | ||
| var invokeJavaScriptTarget = new TestExceptionMethods(); | ||
| hybridWebView.SetInvokeJavaScriptTarget(invokeJavaScriptTarget); | ||
|
|
||
| // Tell JavaScript to invoke the async method that throws an exception | ||
| hybridWebView.SendRawMessage("ThrowExceptionAsync"); | ||
|
|
||
| // Wait for JavaScript to handle the exception | ||
| await WebViewHelpers.WaitForHtmlStatusSet(hybridWebView); | ||
|
|
||
| // Check that JavaScript caught the async exception | ||
| var caughtError = await hybridWebView.EvaluateJavaScriptAsync("GetLastError()"); | ||
| Assert.Equal("Async test exception", caughtError); | ||
|
|
||
| // Check that the method was called before throwing | ||
| Assert.Equal("ThrowExceptionAsync", invokeJavaScriptTarget.LastMethodCalled); | ||
| }); | ||
|
|
||
| [Fact] | ||
| public Task CSharpMethodThatThrowsCustomException_ShouldIncludeExceptionDetails() => | ||
| RunTest("exception-tests.html", async (hybridWebView) => | ||
| { | ||
| var invokeJavaScriptTarget = new TestExceptionMethods(); | ||
| hybridWebView.SetInvokeJavaScriptTarget(invokeJavaScriptTarget); | ||
|
|
||
| // Tell JavaScript to invoke the method that throws a custom exception | ||
| hybridWebView.SendRawMessage("ThrowCustomException"); | ||
|
|
||
| // Wait for JavaScript to handle the exception | ||
| await WebViewHelpers.WaitForHtmlStatusSet(hybridWebView); | ||
|
|
||
| // Check that JavaScript caught the exception with custom details | ||
| var errorType = await hybridWebView.EvaluateJavaScriptAsync("GetLastErrorType()"); | ||
| var errorMessage = await hybridWebView.EvaluateJavaScriptAsync("GetLastErrorMessage()"); | ||
|
|
||
| Assert.Equal("ArgumentException", errorType); | ||
| Assert.Equal("Custom argument exception", errorMessage); | ||
| }); | ||
|
|
||
| [Fact] | ||
| public Task CSharpMethodThatSucceeds_ShouldStillWorkNormally() => | ||
| RunTest("exception-tests.html", async (hybridWebView) => | ||
| { | ||
| var invokeJavaScriptTarget = new TestExceptionMethods(); | ||
| hybridWebView.SetInvokeJavaScriptTarget(invokeJavaScriptTarget); | ||
|
|
||
| // Tell JavaScript to invoke a normal method that doesn't throw | ||
| hybridWebView.SendRawMessage("SuccessMethod"); | ||
|
|
||
| // Wait for JavaScript to complete | ||
| await WebViewHelpers.WaitForHtmlStatusSet(hybridWebView); | ||
|
|
||
| // Check that JavaScript got the normal result | ||
| var result = await hybridWebView.EvaluateJavaScriptAsync("GetLastResult()"); | ||
| Assert.Contains("Success!", result, StringComparison.Ordinal); | ||
|
|
||
| // Check that no error was thrown | ||
| var hasError = await hybridWebView.EvaluateJavaScriptAsync("HasError()"); | ||
| Assert.Contains("false", hasError, StringComparison.Ordinal); | ||
| }); | ||
|
|
||
| private class TestExceptionMethods | ||
| { | ||
| public string? LastMethodCalled { get; private set; } | ||
|
|
||
| public void ThrowException() | ||
| { | ||
| LastMethodCalled = nameof(ThrowException); | ||
| throw new InvalidOperationException("Test exception message"); | ||
| } | ||
|
|
||
| public void ThrowCustomException() | ||
| { | ||
| LastMethodCalled = nameof(ThrowCustomException); | ||
| throw new ArgumentException("Custom argument exception"); | ||
| } | ||
|
|
||
| public async Task ThrowExceptionAsync() | ||
| { | ||
| LastMethodCalled = nameof(ThrowExceptionAsync); | ||
| await Task.Delay(10); // Make it actually async | ||
| throw new InvalidOperationException("Async test exception"); | ||
| } | ||
|
|
||
| public string SuccessMethod() | ||
| { | ||
| LastMethodCalled = nameof(SuccessMethod); | ||
| return "Success!"; | ||
| } | ||
| } | ||
| } | ||
75 changes: 75 additions & 0 deletions
75
src/Controls/tests/DeviceTests/Resources/Raw/HybridTestRoot/exception-tests.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <!DOCTYPE html> | ||
|
|
||
| <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title></title> | ||
| <link rel="icon" href="data:,"> | ||
| <script src="_framework/hybridwebview.js"></script> | ||
| <script> | ||
| window.addEventListener( | ||
| "HybridWebViewMessageReceived", | ||
| async function (e) { | ||
| var methodToInvoke = e.detail.message; | ||
| var result = null; | ||
| var error = null; | ||
| var hasError = false; | ||
|
|
||
| try { | ||
| // Invoke the requested method | ||
| result = await window.HybridWebView.InvokeDotNet(methodToInvoke); | ||
| } catch (ex) { | ||
| hasError = true; | ||
| error = { | ||
| message: ex.message, | ||
| type: ex.dotNetErrorType || ex.name, | ||
mattleibow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| stackTrace: ex.dotNetStackTrace || ex.stack | ||
| }; | ||
| console.error('Caught exception from .NET method:', ex); | ||
| } | ||
|
|
||
| // Store the results so they can be checked in the test | ||
| lastResult = result; | ||
| lastError = error; | ||
| lastHasError = hasError; | ||
| SetStatusDone(); | ||
| }); | ||
|
|
||
| var lastResult = null; | ||
| var lastError = null; | ||
| var lastHasError = false; | ||
|
|
||
| function GetLastResult() { | ||
| return lastResult === null ? null : JSON.stringify(lastResult); | ||
| } | ||
|
|
||
| function GetLastError() { | ||
| return lastError ? lastError.message : null; | ||
| } | ||
|
|
||
| function GetLastErrorType() { | ||
| return lastError ? lastError.type : null; | ||
| } | ||
|
|
||
| function GetLastErrorMessage() { | ||
| return lastError ? lastError.message : null; | ||
| } | ||
|
|
||
| function HasError() { | ||
| return lastHasError.toString(); | ||
| } | ||
|
|
||
| function SetStatusDone() { | ||
| document.getElementById('status').innerHTML = "Done!"; | ||
| } | ||
|
|
||
| </script> | ||
| </head> | ||
| <body> | ||
| <div> | ||
| Hybrid exception test! | ||
| </div> | ||
| <div id="htmlLoaded"></div> | ||
| <div id="status"></div> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.