diff --git a/NativeScript/runtime/ModuleInternal.mm b/NativeScript/runtime/ModuleInternal.mm index ae56a9bf..6a8697ff 100644 --- a/NativeScript/runtime/ModuleInternal.mm +++ b/NativeScript/runtime/ModuleInternal.mm @@ -885,11 +885,21 @@ ScriptOrigin origin(isolate, urlString, 0, 0, false, -1, Local(), false, if (state == Promise::kRejected) { RemoveModuleFromRegistry(canonicalPath); logPhase("evaluate", "promise-rejected"); + if (!promiseTc.HasCaught()) { + // With top-level-await semantics, evaluation errors don't throw into + // the TryCatch — they surface only as the rejection reason of the + // evaluation promise. Re-throw the reason on the isolate so the + // TryCatch captures it and the exception below carries the original + // error message and stack trace instead of a synthetic placeholder. + isolate->ThrowException(promise->Result()); + } if (promiseTc.HasCaught()) { + if (RuntimeConfig.IsDebug) { + Log(@"Error evaluating ES module (rejected promise): %s", canonicalPath.c_str()); + tns::LogError(isolate, promiseTc); + } throw NativeScriptException(isolate, promiseTc, "Module evaluation promise rejected"); } else { - Local reason = promise->Result(); - isolate->ThrowException(reason); throw NativeScriptException(isolate, "Module evaluation promise rejected"); } }