dotnet/maui · info · Error

This is an async test error thrown from JavaScript code!

Error message

This is an async test error thrown from JavaScript code!

What it means

A deliberate asynchronous JavaScript Error thrown by the sample's ThrowJavaScriptErrorAsync() function after a 100ms setTimeout. Like its sync counterpart it exists only to verify that C# can catch exceptions from async JS invoked through the HybridWebView bridge. It is sample/test code.

Source

Thrown at src/Controls/samples/Controls.Sample/Resources/Raw/HybridSamplePage/index.html:131

            try {
                const result = await window.HybridWebView.InvokeDotNet('ThrowExceptionAsync');
                LogMessage("ThrowExceptionAsync unexpectedly succeeded with result: " + result);
            } catch (ex) {
                LogMessage("Caught async exception from ThrowExceptionAsync: " + ex.message);
                LogMessage("Exception type: " + (ex.dotNetErrorType || "Unknown"));
            }
        }

        // JavaScript functions that throw exceptions (for testing C# exception handling of JS calls)
        function ThrowJavaScriptError() {
            LogMessage("ThrowJavaScriptError called - about to throw");
            throw new Error("This is a test error thrown from JavaScript code!");
        }

        async function ThrowJavaScriptErrorAsync() {
            LogMessage("ThrowJavaScriptErrorAsync called - about to throw");
            await new Promise(resolve => setTimeout(resolve, 100));
            throw new Error("This is an async test error thrown from JavaScript code!");
        }

    </script>
</head>
<body>
    <div>
        Hybrid sample!
    </div>
    <div>
        <button onclick="window.HybridWebView.SendRawMessage('Message from JS! ' + (count++))">Send message to C#</button>
    </div>
    <div>
        <button onclick="InvokeDoSyncWork()">Call C# sync method (no params)</button>
        <button onclick="InvokeDoSyncWorkParams()">Call C# sync method (params)</button>
        <button onclick="InvokeDoSyncWorkReturn()">Call C# method (no params) and get simple return value</button>
        <button onclick="InvokeDoSyncWorkParamsReturn()">Call C# method (params) and get complex return value</button>
    </div>
    <div>

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. If you reused the sample HTML, remove ThrowJavaScriptErrorAsync or unwire it from production UI.
  2. If testing, await the bridge call inside try/catch on the C# side to observe the rejection.

Example fix

// before
async function ThrowJavaScriptErrorAsync() {
  await new Promise(resolve => setTimeout(resolve, 100));
  throw new Error("This is an async test error thrown from JavaScript code!");
}

// after
async function DoRealAsyncWork() {
  await new Promise(resolve => setTimeout(resolve, 100));
  return "ok";
}
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    await hybridWebView.EvaluateJavaScriptAsync("ThrowJavaScriptErrorAsync()");
}
catch (Exception ex)
{
    Debug.WriteLine($"Async JS rejected: {ex.Message}");
}

Prevention

When it happens

Trigger: C# invokes the async function through the JS bridge, or the sample's 'Test JS Async Exception' button is clicked, causing the awaited promise to reject after the timeout.

Common situations: Running the sample JS-exception test; copying the sample HTML and leaving the demo async throw reachable; automated tests that walk sample buttons.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/eb04d4d51841e923. Report an issue: GitHub.