fix: ensure that errors thrown in the context bridge are created in the correct context (#24534)
This commit is contained in:
parent
b500294c1d
commit
5cfe956fe1
2 changed files with 63 additions and 8 deletions
|
@ -500,6 +500,39 @@ describe('contextBridge', () => {
|
|||
expect(threw).to.equal(true);
|
||||
});
|
||||
|
||||
it('should copy thrown errors into the other context', async () => {
|
||||
await makeBindingWindow(() => {
|
||||
contextBridge.exposeInMainWorld('example', {
|
||||
throwNormal: () => {
|
||||
throw new Error('whoops');
|
||||
},
|
||||
throwWeird: () => {
|
||||
throw 'this is no error...'; // eslint-disable-line no-throw-literal
|
||||
},
|
||||
throwNotClonable: () => {
|
||||
return Object(Symbol('foo'));
|
||||
},
|
||||
argumentConvert: () => {}
|
||||
});
|
||||
});
|
||||
const result = await callWithBindings((root: any) => {
|
||||
const getError = (fn: Function) => {
|
||||
try {
|
||||
fn();
|
||||
} catch (e) {
|
||||
return e;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
const normalIsError = Object.getPrototypeOf(getError(root.example.throwNormal)) === Error.prototype;
|
||||
const weirdIsError = Object.getPrototypeOf(getError(root.example.throwWeird)) === Error.prototype;
|
||||
const notClonableIsError = Object.getPrototypeOf(getError(root.example.throwNotClonable)) === Error.prototype;
|
||||
const argumentConvertIsError = Object.getPrototypeOf(getError(() => root.example.argumentConvert(Object(Symbol('test'))))) === Error.prototype;
|
||||
return [normalIsError, weirdIsError, notClonableIsError, argumentConvertIsError];
|
||||
});
|
||||
expect(result).to.deep.equal([true, true, true, true], 'should all be errors in the current context');
|
||||
});
|
||||
|
||||
it('should not leak prototypes', async () => {
|
||||
await makeBindingWindow(() => {
|
||||
contextBridge.exposeInMainWorld('example', {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue