Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.
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
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