Skip to content

Commit

Permalink
Size optimizations for error propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
developit committed May 25, 2018
1 parent 62bc83f commit 2a5c1c2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/rpc-worker-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ function workerSetup() {
p.then(result => {
postMessage({ type: 'RPC', id, result });
})
.catch(error => {
if (error.stack) {
postMessage({ type: 'RPC', id, error: { name: error.name, message: error.message, stack: error.stack } });
return;
.catch(e => {
let error = { message: e};
if (e.stack) {
error.message = e.message;
error.stack = e.stack;
error.name = e.name;
}
postMessage({ type: 'RPC', id, error });
});
Expand Down
9 changes: 4 additions & 5 deletions src/rpc-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ export default function addMethods(worker, methods) {
if (f) {
delete callbacks[d.id];
if (d.error) {
let workerError = Error(d.error && d.error.message ? d.error.message : 'Error in worker');
if (d.error && d.error.stack) workerError.stack = d.error.stack;
if (d.error && d.error.name) workerError.name = d.error.name;
f[1](workerError);
f[1](Object.assign(Error(d.error.message), d.error));
}
else {
f[0](d.result);
}
else f[0](d.result);
}
}
else {
Expand Down
7 changes: 4 additions & 3 deletions test/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ describe('worker', () => {

it('worker.throwError() should pass the Error back to the application context', async () => {
try {
let out = await worker.throwError();
} catch (e) {
expect(e).toEqual(new Error('Error in worker.js'));
await worker.throwError();
}
catch (e) {
expect(e).toEqual(Error('Error in worker.js'));
}
});

Expand Down

0 comments on commit 2a5c1c2

Please sign in to comment.