diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 3262b15c7b6ab8..c17ca6ef992c39 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -125,7 +125,14 @@ class ContextifyContext { int length = names->Length(); for (int i = 0; i < length; i++) { Local key = names->Get(i)->ToString(env()->isolate()); - bool has = sandbox_obj->HasOwnProperty(context, key).FromJust(); + auto maybe_has = sandbox_obj->HasOwnProperty(context, key); + + // Check for pending exceptions + if (!maybe_has.IsJust()) + break; + + bool has = maybe_has.FromJust(); + if (!has) { // Could also do this like so: // diff --git a/test/parallel/test-vm-proxies.js b/test/parallel/test-vm-proxies.js index d47f7508213534..d908d713748f7f 100644 --- a/test/parallel/test-vm-proxies.js +++ b/test/parallel/test-vm-proxies.js @@ -16,3 +16,16 @@ sandbox = { Proxy: Proxy }; vm.runInNewContext('this.Proxy = Proxy', sandbox); assert.strictEqual(typeof sandbox.Proxy, 'function'); assert.strictEqual(sandbox.Proxy, Proxy); + +// Handle a sandbox that throws while copying properties +assert.throws(() => { + const handler = { + getOwnPropertyDescriptor: (target, prop) => { + throw new Error('whoops'); + } + }; + const sandbox = new Proxy({foo: 'bar'}, handler); + const context = vm.createContext(sandbox); + + vm.runInContext('', context); +}, /whoops/);