Fix error handling in worker

This commit is contained in:
Alexander Zinchuk 2021-04-26 15:33:07 +03:00
parent 3ea59fbc17
commit 9b8eb488c3
2 changed files with 9 additions and 10 deletions

View File

@ -87,17 +87,16 @@ onmessage = async (message: OriginMessageEvent) => {
};
function handleErrors() {
// eslint-disable-next-line no-restricted-globals
self.onerror = (e) => {
// eslint-disable-next-line no-console
console.error(e);
sendToOrigin({ type: 'unhandledError', error: { message: e.message || 'Uncaught exception in worker' } });
sendToOrigin({ type: 'unhandledError', error: { message: e.error.message || 'Uncaught exception in worker' } });
};
// eslint-disable-next-line no-restricted-globals
self.addEventListener('unhandledrejection', (e) => {
// eslint-disable-next-line no-console
console.error(e);
sendToOrigin({ type: 'unhandledError', error: { message: e.reason || 'Uncaught rejection in worker' } });
sendToOrigin({ type: 'unhandledError', error: { message: e.reason.message || 'Uncaught rejection in worker' } });
});
}

View File

@ -77,16 +77,16 @@ export default function createInterface(api: Record<string, Function>) {
}
function handleErrors() {
self.onerror = (err) => {
self.onerror = (e) => {
// eslint-disable-next-line no-console
console.error(err);
sendToOrigin({ type: 'unhandledError', error: { message: 'Uncaught exception in worker' } });
console.error(e);
sendToOrigin({ type: 'unhandledError', error: { message: e.error.message || 'Uncaught exception in worker' } });
};
self.addEventListener('unhandledrejection', (err) => {
self.addEventListener('unhandledrejection', (e) => {
// eslint-disable-next-line no-console
console.error(err);
sendToOrigin({ type: 'unhandledError', error: { message: 'Uncaught rejection in worker' } });
console.error(e);
sendToOrigin({ type: 'unhandledError', error: { message: e.reason.message || 'Uncaught rejection in worker' } });
});
}