Skip to content

Commit b3ef656

Browse files
[release/7.0-rc1] [wasm] Unwrap exception when calling entrypoint (#74263)
* Unwrap exception, * WBT. Co-authored-by: Marek Fišera <[email protected]>
1 parent 8196f9a commit b3ef656

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/JavaScriptExports.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public static void CallEntrypoint(JSMarshalerArgument* arguments_buffer)
8585
}
8686
catch (Exception ex)
8787
{
88+
if (ex is TargetInvocationException refEx && refEx.InnerException != null)
89+
ex = refEx.InnerException;
90+
8891
arg_exc.ToJS(ex);
8992
}
9093
}

src/tests/BuildWasmApps/Wasm.Build.Tests/WasmBuildAppTest.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ public static int Main()
5555
}
5656
}", buildArgs, host, id);
5757

58+
[Theory]
59+
[MemberData(nameof(MainMethodTestData), parameters: new object[] { /*aot*/ false, RunHost.All })]
60+
public void ExceptionFromMain(BuildArgs buildArgs, RunHost host, string id)
61+
=> TestMain("main_exception", """
62+
using System;
63+
using System.Threading.Tasks;
64+
65+
public class TestClass {
66+
public static int Main() => throw new Exception("MessageFromMyException");
67+
}
68+
""", buildArgs, host, id, expectedExitCode: 71, expectedOutput: "Error: MessageFromMyException");
69+
5870
private static string s_bug49588_ProgramCS = @"
5971
using System;
6072
public class TestClass {
@@ -165,7 +177,9 @@ protected void TestMain(string projectName,
165177
RunHost host,
166178
string id,
167179
string extraProperties = "",
168-
bool? dotnetWasmFromRuntimePack = null)
180+
bool? dotnetWasmFromRuntimePack = null,
181+
int expectedExitCode = 42,
182+
string expectedOutput = "Hello, World!")
169183
{
170184
buildArgs = buildArgs with { ProjectName = projectName };
171185
buildArgs = ExpandBuildArgs(buildArgs, extraProperties);
@@ -179,8 +193,8 @@ protected void TestMain(string projectName,
179193
InitProject: () => File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), programText),
180194
DotnetWasmFromRuntimePack: dotnetWasmFromRuntimePack));
181195

182-
RunAndTestWasmApp(buildArgs, expectedExitCode: 42,
183-
test: output => Assert.Contains("Hello, World!", output), host: host, id: id);
196+
RunAndTestWasmApp(buildArgs, expectedExitCode: expectedExitCode,
197+
test: output => Assert.Contains(expectedOutput, output), host: host, id: id);
184198
}
185199
}
186200
}

0 commit comments

Comments
 (0)