Skip to content

Commit

Permalink
fix: don't handle window.onerror
Browse files Browse the repository at this point in the history
  • Loading branch information
wqcstrong committed Jun 15, 2023
1 parent 7799aaf commit ae47966
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 90 deletions.
43 changes: 4 additions & 39 deletions src/plugins/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,10 @@ export default class ErrorPlugin implements PageSpyPlugin {
}

private onUncaughtError() {
const userErr = window.onerror;
// @ts-ignore
const isConfigurable = delete window.onerror;
if (!isConfigurable) {
window.onerror = (...args) => {
ErrorPlugin.sendMessage(args[4]);
if (userErr) {
userErr.apply(window, args);
}
};
return;
}

let errorHandler: (this: Window, ev: ErrorEvent) => any;
Object.defineProperty(window, 'onerror', {
// Normally, users would simply capture errors by assigning to 'window.onerror',
// but to avoid conflicts with the logic of other libraries, we specify
// 'configurable' as false here.
configurable: false,
enumerable: true,
get() {
return errorHandler;
},
set(fn: OnErrorEventHandler) {
window.removeEventListener('error', errorHandler);
errorHandler = (e: ErrorEvent) => {
ErrorPlugin.sendMessage(e.error?.stack || e.message);
fn?.apply(window, [
e.message,
e.filename,
e.lineno,
e.colno,
e.error,
]);
};
window.addEventListener('error', errorHandler);
},
});
window.onerror = userErr;
const errorHandler = (e: ErrorEvent) => {
ErrorPlugin.sendMessage(e.error?.stack || e.message);
};
window.addEventListener('error', errorHandler);
}

private onResourceLoadError() {
Expand Down
53 changes: 2 additions & 51 deletions tests/plugins/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,9 @@ afterEach(() => {
});

describe('Error plugin', () => {
describe('Uncaught Error', () => {
it('Have initiator value', (done) => {
expect(window.onerror).not.toBeFalsy();

setTimeout(() => {
throw new Error('Unit test');
});
setTimeout(() => {
expect(errorOccupied).toHaveBeenCalledTimes(1);
done();
}, 10);
});
it('Register new error function', (done) => {
const fn = jest.fn();
window.onerror = fn;
setTimeout(() => {
throw new Error('Unit test');
});

setTimeout(() => {
expect(fn).toHaveBeenCalledTimes(1);
expect(errorOccupied).toHaveBeenCalledTimes(1);
done();
}, 10);
});
it('Only the last registration will take effect if assign many times', (done) => {
const fn1 = jest.fn();
const fn2 = jest.fn();
const fn3 = jest.fn();
window.onerror = fn1;
window.onerror = fn2;
window.onerror = fn3;

setTimeout(() => {
throw new Error('Unit test');
});
setTimeout(() => {
expect([
fn1.mock.calls.length,
fn2.mock.calls.length,
fn3.mock.calls.length,
]).toEqual([0, 0, 1]);
done();
}, 10);
});
});
it('Resource load failed error', () => {
it('Trigger error', () => {
window.dispatchEvent(new Event('error'));
expect(errorOccupied).toHaveBeenCalledTimes(1);
});
it('Unhandledrejection error', () => {
window.dispatchEvent(new Event('unhandledrejection'));
expect(errorOccupied).toHaveBeenCalledTimes(1);
expect(errorOccupied).toHaveBeenCalledTimes(3);
});
});

0 comments on commit ae47966

Please sign in to comment.