Skip to content

Commit 2a5c1c2

Browse files
committed
Size optimizations for error propagation
1 parent 62bc83f commit 2a5c1c2

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

src/rpc-worker-loader.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ function workerSetup() {
1313
p.then(result => {
1414
postMessage({ type: 'RPC', id, result });
1515
})
16-
.catch(error => {
17-
if (error.stack) {
18-
postMessage({ type: 'RPC', id, error: { name: error.name, message: error.message, stack: error.stack } });
19-
return;
16+
.catch(e => {
17+
let error = { message: e};
18+
if (e.stack) {
19+
error.message = e.message;
20+
error.stack = e.stack;
21+
error.name = e.name;
2022
}
2123
postMessage({ type: 'RPC', id, error });
2224
});

src/rpc-wrapper.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ export default function addMethods(worker, methods) {
99
if (f) {
1010
delete callbacks[d.id];
1111
if (d.error) {
12-
let workerError = Error(d.error && d.error.message ? d.error.message : 'Error in worker');
13-
if (d.error && d.error.stack) workerError.stack = d.error.stack;
14-
if (d.error && d.error.name) workerError.name = d.error.name;
15-
f[1](workerError);
12+
f[1](Object.assign(Error(d.error.message), d.error));
13+
}
14+
else {
15+
f[0](d.result);
1616
}
17-
else f[0](d.result);
1817
}
1918
}
2019
else {

test/src/index.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ describe('worker', () => {
2121

2222
it('worker.throwError() should pass the Error back to the application context', async () => {
2323
try {
24-
let out = await worker.throwError();
25-
} catch (e) {
26-
expect(e).toEqual(new Error('Error in worker.js'));
24+
await worker.throwError();
25+
}
26+
catch (e) {
27+
expect(e).toEqual(Error('Error in worker.js'));
2728
}
2829
});
2930

0 commit comments

Comments
 (0)