{"record":{"id":"eb04d4d51841e923","repo":"dotnet/maui","slug":"this-is-an-async-test-error-thrown-from-javascript","errorCode":null,"errorMessage":"This is an async test error thrown from JavaScript code!","messagePattern":"This is an async test error thrown from JavaScript code!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"info","filePath":"src/Controls/samples/Controls.Sample/Resources/Raw/HybridSamplePage/index.html","lineNumber":131,"sourceCode":"            try {\n                const result = await window.HybridWebView.InvokeDotNet('ThrowExceptionAsync');\n                LogMessage(\"ThrowExceptionAsync unexpectedly succeeded with result: \" + result);\n            } catch (ex) {\n                LogMessage(\"Caught async exception from ThrowExceptionAsync: \" + ex.message);\n                LogMessage(\"Exception type: \" + (ex.dotNetErrorType || \"Unknown\"));\n            }\n        }\n\n        // JavaScript functions that throw exceptions (for testing C# exception handling of JS calls)\n        function ThrowJavaScriptError() {\n            LogMessage(\"ThrowJavaScriptError called - about to throw\");\n            throw new Error(\"This is a test error thrown from JavaScript code!\");\n        }\n\n        async function ThrowJavaScriptErrorAsync() {\n            LogMessage(\"ThrowJavaScriptErrorAsync called - about to throw\");\n            await new Promise(resolve => setTimeout(resolve, 100));\n            throw new Error(\"This is an async test error thrown from JavaScript code!\");\n        }\n\n    </script>\n</head>\n<body>\n    <div>\n        Hybrid sample!\n    </div>\n    <div>\n        <button onclick=\"window.HybridWebView.SendRawMessage('Message from JS! ' + (count++))\">Send message to C#</button>\n    </div>\n    <div>\n        <button onclick=\"InvokeDoSyncWork()\">Call C# sync method (no params)</button>\n        <button onclick=\"InvokeDoSyncWorkParams()\">Call C# sync method (params)</button>\n        <button onclick=\"InvokeDoSyncWorkReturn()\">Call C# method (no params) and get simple return value</button>\n        <button onclick=\"InvokeDoSyncWorkParamsReturn()\">Call C# method (params) and get complex return value</button>\n    </div>\n    <div>","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/samples/Controls.Sample/Resources/Raw/HybridSamplePage/index.html#L113-L149","documentation":"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.","triggerScenarios":"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.","commonSituations":"Running the sample JS-exception test; copying the sample HTML and leaving the demo async throw reachable; automated tests that walk sample buttons.","solutions":["If you reused the sample HTML, remove ThrowJavaScriptErrorAsync or unwire it from production UI.","If testing, await the bridge call inside try/catch on the C# side to observe the rejection."],"exampleFix":"// before\nasync function ThrowJavaScriptErrorAsync() {\n  await new Promise(resolve => setTimeout(resolve, 100));\n  throw new Error(\"This is an async test error thrown from JavaScript code!\");\n}\n\n// after\nasync function DoRealAsyncWork() {\n  await new Promise(resolve => setTimeout(resolve, 100));\n  return \"ok\";\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    await hybridWebView.EvaluateJavaScriptAsync(\"ThrowJavaScriptErrorAsync()\");\n}\ncatch (Exception ex)\n{\n    Debug.WriteLine($\"Async JS rejected: {ex.Message}\");\n}","preventionTips":["Strip demo async-throw functions from production HTML.","Await all JS-evaluation calls so promise rejections propagate to C#.","Keep test/demo JS behind a clear naming or build flag."],"tags":["hybridwebview","javascript","async","sample","exception-handling"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}