Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 8, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

HybridWebView Bi-Directional Exception Handling

This PR implements comprehensive exception handling between C# and JavaScript in HybridWebView, enabling proper error propagation in both directions with rich debugging information.

Key Features

🔄 Bi-Directional Exception Flow

  • C# → JavaScript: Exceptions thrown in C# methods are properly caught and converted to JavaScript Error objects with detailed information (message, type, stack trace)
  • JavaScript → C#: JavaScript errors are captured and wrapped in HybridWebViewInvokeJavaScriptException with preserved error context

🛠️ Rich Error Information

  • Exception types, messages, and stack traces preserved across platform boundaries
  • JavaScript errors include original error name and stack trace
  • C# exceptions include full type information and stack traces

🧪 Comprehensive Testing

  • Unit tests covering synchronous and asynchronous exception scenarios
  • Interactive sample application with demo buttons for both directions
  • Automated test infrastructure for reliable validation

✅ Production Ready

  • Zero breaking changes - all existing code continues to work
  • Backward compatible error logging maintained
  • Proper exception unwrapping with ExceptionDispatchInfo to preserve stack traces
  • Progressive enhancement - applications can opt into enhanced error handling

Usage Examples

C# Exceptions in JavaScript:

try {
    await window.HybridWebView.InvokeDotNet('ThrowingMethod');
} catch (error) {
    console.log(error.message);        // "Data cannot be processed"  
    console.log(error.dotNetErrorType); // "ArgumentException"
    console.log(error.dotNetStackTrace); // Full .NET stack trace
}

JavaScript Exceptions in C#:

try {
    var result = await hybridWebView.InvokeJavaScriptAsync<string>("throwingJsFunction");
}
catch (HybridWebViewInvokeJavaScriptException ex) {
    Console.WriteLine($"JS Error: {ex.Message}");
    Console.WriteLine($"JS Type: {ex.Data["JavaScriptErrorName"]}");
    Console.WriteLine($"JS Stack: {ex.StackTrace}");
}

This enables robust error handling patterns in hybrid applications and provides developers with the debugging information needed to diagnose issues across the C#/JavaScript boundary.

Fixes #27096


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Right now HybridWebView invocation of C# methods can hide exceptions thrown by the user code. At an absolute minimum this needs to be logged via .NET MAUI's loggers and/or the webview loggers (JS console.log(), etc.). But some of these exceptions sho... Fix HybridWebView to properly bubble C# exceptions to JavaScript Sep 8, 2025
Copilot AI requested a review from mattleibow September 8, 2025 15:12
Copilot finished work on behalf of mattleibow September 8, 2025 15:12
@mattleibow mattleibow force-pushed the copilot/fix-cc3b23cd-67af-45d5-96c5-200f2371b602 branch 2 times, most recently from 74326b6 to 4014e40 Compare September 9, 2025 01:38
Copilot AI requested a review from mattleibow September 9, 2025 02:41
Copilot finished work on behalf of mattleibow September 9, 2025 02:41
@mattleibow mattleibow added this to the .NET 10.0-rc2 milestone Sep 9, 2025
@mattleibow mattleibow added the p/0 Current heighest priority issues that we are targeting for a release. label Sep 9, 2025
@mattleibow
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@mattleibow
Copy link
Member

/rebase

@github-actions github-actions bot force-pushed the copilot/fix-cc3b23cd-67af-45d5-96c5-200f2371b602 branch from 26c3d4b to 527a213 Compare September 9, 2025 20:31
@mattleibow
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@mattleibow
Copy link
Member

@copilot can you look at the code and update the title and description. I think it is now out of date. Start the title and description fresh please.

Copy link
Contributor Author

Copilot AI commented Sep 9, 2025

@copilot can you look at the code and update the title and description. I think it is now out of date. Start the title and description fresh please.

I've updated the PR title and description to accurately reflect the current implementation. The PR now covers comprehensive bi-directional exception handling between C# and JavaScript, which is much broader than the original focus. Updated with commit 527a213.

Copilot AI changed the title Fix HybridWebView to properly bubble C# exceptions to JavaScript Enable Bi-Directional Exception Handling in HybridWebView Sep 9, 2025
Copilot finished work on behalf of mattleibow September 9, 2025 20:40
@mattleibow mattleibow marked this pull request as ready for review September 9, 2025 21:14
Copilot AI review requested due to automatic review settings September 9, 2025 21:14
@mattleibow mattleibow requested a review from a team as a code owner September 9, 2025 21:14
Copilot AI changed the title Enable Bi-Directional Exception Handling in HybridWebView Complete Bi-Directional Exception Handling System for HybridWebView Sep 10, 2025
Copilot finished work on behalf of mattleibow September 10, 2025 09:34
Copy link
Contributor Author

Copilot AI commented Sep 10, 2025

@copilot can you look at the code and update the title and description. I think it is now out of date. Start the title and description fresh please.

I've updated the PR title and description to accurately reflect the current comprehensive bi-directional exception handling implementation. The new title "HybridWebView Bi-Directional Exception Handling" better captures what this PR delivers, and the description is now more focused and highlights the key features:

  • Comprehensive bi-directional exception flow (C# ↔ JavaScript)
  • Rich error information preserved across platform boundaries
  • Production-ready with zero breaking changes
  • Extensive testing coverage and interactive samples

The description now emphasizes the practical benefits and usage examples rather than implementation details, making it clearer for developers what this feature provides.

Copilot AI changed the title Complete Bi-Directional Exception Handling System for HybridWebView HybridWebView Bi-Directional Exception Handling Sep 10, 2025
Copilot finished work on behalf of mattleibow September 10, 2025 09:42
Copilot AI and others added 6 commits September 11, 2025 02:22
- Extended DotNetInvokeResult to include error information fields
- Modified InvokeDotNetAsync to return structured error responses instead of null
- Updated TypeScript/JavaScript to detect and throw errors from C# responses
- Added comprehensive tests for exception handling scenarios
- Created test HTML page for exception testing

Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
- Added ThrowException and ThrowExceptionAsync methods to demonstrate new functionality
- Added JavaScript functions to test and showcase exception catching
- Added UI buttons to trigger exception handling tests
- Updated sample to show both error and success scenarios

Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
@mattleibow mattleibow force-pushed the copilot/fix-cc3b23cd-67af-45d5-96c5-200f2371b602 branch from 527a213 to 7fb8324 Compare September 11, 2025 00:23
@mattleibow
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@mattleibow mattleibow moved this from Todo to Ready To Review in MAUI SDK Ongoing Sep 11, 2025
@mattleibow
Copy link
Member

Unrelated failing tests are unrelated and thus not failing. :)

@jsuarezruiz jsuarezruiz added the area-controls-hybridwebview HybridWebView control label Sep 12, 2025
// invoke the .NET method
return dotnetMethod.Invoke(jsInvokeTarget, invokeParamValues);
}
catch (TargetInvocationException tie) // unwrap while preserving original stack trace
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can simplify it using ThrowIfNotNull:

catch (TargetInvocationException tie)
{
    // This will rethrow the inner exception if it exists, preserving stack trace
    ExceptionDispatchInfo.Capture(tie.InnerException).ThrowIfNotNull();
    
    // no inner exception; rethrow the TargetInvocationException itself preserving its stack
    throw;
}

@github-project-automation github-project-automation bot moved this from Ready To Review to Approved in MAUI SDK Ongoing Sep 12, 2025
@PureWeen PureWeen merged commit 9f0c4b7 into net10.0 Sep 12, 2025
149 checks passed
@PureWeen PureWeen deleted the copilot/fix-cc3b23cd-67af-45d5-96c5-200f2371b602 branch September 12, 2025 18:14
@github-project-automation github-project-automation bot moved this from Approved to Done in MAUI SDK Ongoing Sep 12, 2025
@mattleibow mattleibow linked an issue Sep 25, 2025 that may be closed by this pull request
@github-actions github-actions bot locked and limited conversation to collaborators Oct 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-hybridwebview HybridWebView control p/0 Current heighest priority issues that we are targeting for a release.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants