Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Error cloning error in Worker promise/rejection case #45

Merged
merged 2 commits into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/contexts/BrowserWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
case 'console':
window.console && window.console[data.method].apply(window.console, data.args);
break;
case 'deferred_reject_error':
this.deferreds[data.token].reject(data.error);
break;
case 'result':

var callback = this.callbacks[data.token];
Expand Down Expand Up @@ -275,6 +278,26 @@ function workerBoilerScript() {
return def;
}
function reject(r, transfers) {
if (r instanceof Error) {
// Create an error object that can be cloned: (See #44/#45):
var cloneableError = {
message: r.message,
stack: r.stack,
name: r.name,
code: r.code
};
for (var i in r) {
if (r.hasOwnProperty(i)) {
cloneableError[i] = r[i];
}
}
postMessage({
cmd: 'deferred_reject_error',
token: data.token,
error: cloneableError
});
return;
}
returnResult({
isDeferred: true,
action: 'reject',
Expand Down
16 changes: 16 additions & 0 deletions test/operative.iframe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ describe('Operative (forced iframe context)', function() {
});
});
});
describe('Rejecting with an error', function() {
it('Should reject correctly', function(done) {
var op = operative(function() {
var deferred = this.deferred();
deferred.reject(new Error('foo 789'));
});
op().then(function() {
expect(true).to.be.false; // fail
done();
}).catch(function(err) {
console.log(err);
expect(err.message).to.equal('foo 789'); // pass
done();
});
});
});
});
});

Expand Down
33 changes: 33 additions & 0 deletions test/operative.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,39 @@ describe('Operative (worker Context)', function() {
});
});
});
describe('Rejecting with an error', function() {
it('Should reject correctly', function(done) {
var op = operative(function() {
var deferred = this.deferred();
deferred.reject(new Error('foo 789'));
});
op().then(function() {
expect(true).to.be.false; // fail
done();
}).catch(function(err) {
expect(err.message).to.equal('foo 789'); // pass
done();
});
});
describe('With additional props', function() {
it('Should reject correctly and be received with props', function(done) {
var op = operative(function() {
var deferred = this.deferred();
var error = new Error('foo');
error.custom = 123;
deferred.reject(error);
});
op().then(function() {
expect(true).to.be.false; // fail
done();
}).catch(function(err) {
expect(err.message).to.equal('foo');
expect(err.custom).to.equal(123);
done();
});
});
});
});
});
});

Expand Down