Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
fix: preserve context in for rejected promises (#428)
Browse files Browse the repository at this point in the history
currently, context is lost in case of an unhandled promise rejection. This fix keeps track of the context by looking into the rejected promise domain. This fix has been tested in my production environment and seems to do the trick there.
  • Loading branch information
urish authored and kamilogorek committed Feb 22, 2018
1 parent 339d21f commit b54fdf2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ extend(Raven.prototype, {

if (this.captureUnhandledRejections) {
var self = this;
global.process.on('unhandledRejection', function(reason) {
self.captureException(reason, function(sendErr, eventId) {
global.process.on('unhandledRejection', function(reason, promise) {
var context = promise.domain && promise.domain.sentryContext;
self.captureException(reason, context || {}, function(sendErr, eventId) {
if (!sendErr) utils.consoleAlert('unhandledRejection captured: ' + eventId);
});
});
Expand Down
53 changes: 53 additions & 0 deletions test/raven.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,59 @@ describe('raven.Client', function() {
}
});

it('should preserve context on unhandledRejection', function(done) {
var listeners = process.listeners('unhandledRejection');
listeners.length.should.equal(0);

var scope = nock('https://app.getsentry.com')
.filteringRequestBody(/.*/, '*')
.post('/api/269/store/', '*')
.reply(200, function(uri, body) {
zlib.inflate(new Buffer(body, 'base64'), function(err, dec) {
if (err) return done(err);
var msg = JSON.parse(dec.toString());

msg.user.should.eql({
id: '123'
});

scope.done();
done();
});
return 'OK';
});

client = new raven.Client(dsn, {captureUnhandledRejections: true});
client.install();

listeners = process.listeners('unhandledRejection');
listeners.length.should.equal(1);

client.context(function() {
client.setContext({
user: {
id: '123'
}
});

// promises didn't include domain property until 8.0.0
// see: https://nodejs.org/api/domain.html#domain_domains_and_promises
// also: https://github.com/nodejs/node/pull/12489
if (process.version >= 'v8.0.0') {
// eslint-disable-next-line no-new
new Promise(function(resolve, reject) {
reject(new Error('rejected!'));
});
} else {
setTimeout(function() {
var error = new Error('rejected!');
var promise = Promise.reject(error);
process.emit('unhandledRejection', error, promise);
});
}
});
});

it('should add itself to the uncaughtException event list', function() {
var listeners = process.listeners('uncaughtException');
listeners.length.should.equal(0);
Expand Down

0 comments on commit b54fdf2

Please sign in to comment.