Skip to content
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

[wasm][test] install handlers for unhandled exceptions on browser #74751

Merged
merged 4 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mono/wasm/Wasm.Build.Tests/WasmTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void UpdateBrowserMainJs()
string mainJsPath = Path.Combine(_projectDir!, "main.js");
string mainJsContent = File.ReadAllText(mainJsPath);

mainJsContent = mainJsContent.Replace(".create()", ".withConsoleForwarding().withElementOnExit().withExitCodeLogging().create()");
mainJsContent = mainJsContent.Replace(".create()", ".withConsoleForwarding().withElementOnExit().withExitCodeLogging().withExitOnUnhandledError().create()");
File.WriteAllText(mainJsPath, mainJsContent);
}

Expand Down
23 changes: 23 additions & 0 deletions src/mono/wasm/runtime/run-outer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ class HostBuilder implements DotnetHostBuilder {
}
}

// internal
withExitOnUnhandledError(): DotnetHostBuilder {
const handler = function fatal_handler(event: Event, error: any) {
event.preventDefault();
try {
mono_exit(1, error);
} catch (err) {
// no not re-throw from the fatal handler
}
};
try {
// it seems that emscripten already does the right thing for NodeJs and that there is no good solution for V8 shell.
if (ENVIRONMENT_IS_WEB) {
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
window.addEventListener("unhandledrejection", (event) => handler(event, event.reason));
window.addEventListener("error", (event) => handler(event, event.error));
}
return this;
} catch (err) {
mono_exit(1, err);
throw err;
}
}

// internal
withAsyncFlushOnExit(): DotnetHostBuilder {
try {
Expand Down
16 changes: 9 additions & 7 deletions src/mono/wasm/runtime/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { ENVIRONMENT_IS_WEB, INTERNAL, Module, runtimeHelpers } from "./imports";
import { ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports";
import { mono_wasm_wait_for_debugger } from "./debug";
import { abort_startup, mono_wasm_set_main_args } from "./startup";
import cwraps from "./cwraps";
Expand Down Expand Up @@ -108,12 +108,14 @@ async function flush_node_streams() {
function set_exit_code_and_quit_now(exit_code: number, reason?: any): void {
if (runtimeHelpers.ExitStatus) {
if (reason && !(reason instanceof runtimeHelpers.ExitStatus)) {
if (reason instanceof Error)
Module.printErr(INTERNAL.mono_wasm_stringify_as_error_with_stack(reason));
else if (typeof reason == "string")
Module.printErr(reason);
else
Module.printErr(JSON.stringify(reason));
if (!runtimeHelpers.config.logExitCode) {
if (reason instanceof Error)
Module.printErr(mono_wasm_stringify_as_error_with_stack(reason));
else if (typeof reason == "string")
Module.printErr(reason);
else
Module.printErr(JSON.stringify(reason));
}
}
else {
reason = new runtimeHelpers.ExitStatus(exit_code);
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/test-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,15 @@ async function run() {
.withVirtualWorkingDirectory(runArgs.workingDirectory)
.withEnvironmentVariables(runArgs.environmentVariables)
.withDiagnosticTracing(runArgs.diagnosticTracing)
.withExitOnUnhandledError()
.withExitCodeLogging()
.withElementOnExit();

if (is_node) {
dotnet
.withEnvironmentVariable("NodeJSPlatform", process.platform)
.withAsyncFlushOnExit();

const modulesToLoad = runArgs.environmentVariables["NPM_MODULES"];
if (modulesToLoad) {
dotnet.withModuleConfig({
Expand Down