Skip to content

Commit 31e51d5

Browse files
authored
feat: capture exception breadcrumbs as we do in raven-js (#461)
1 parent f59a264 commit 31e51d5

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

lib/client.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,12 @@ extend(Raven.prototype, {
232232
kwargs.user = extend({}, globalContext.user, domainContext.user, kwargs.user);
233233
kwargs.tags = extend({}, globalContext.tags, domainContext.tags, kwargs.tags);
234234
kwargs.extra = extend({}, globalContext.extra, domainContext.extra, kwargs.extra);
235+
// Perform a shallow copy of breadcrums to not send one that we'll capture below through as well
235236
kwargs.breadcrumbs = {
236-
values: domainContext.breadcrumbs || globalContext.breadcrumbs || []
237+
values:
238+
(domainContext.breadcrumbs && domainContext.breadcrumbs.slice()) ||
239+
(globalContext.breadcrumbs && globalContext.breadcrumbs.slice()) ||
240+
[]
237241
};
238242

239243
/*
@@ -288,6 +292,15 @@ extend(Raven.prototype, {
288292
kwargs = this.dataCallback(kwargs);
289293
}
290294

295+
// Capture breadcrumb before sending it, as we also want to have it even when
296+
// it was dropped due to sampleRate or shouldSendCallback
297+
this.captureBreadcrumb({
298+
category: 'sentry',
299+
message: kwargs.message,
300+
event_id: kwargs.event_id,
301+
level: kwargs.level || 'error' // presume error unless specified
302+
});
303+
291304
var shouldSend = true;
292305
if (!this._enabled) shouldSend = false;
293306
if (this.shouldSendCallback && !this.shouldSendCallback(kwargs)) shouldSend = false;

test/raven.client.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,32 @@ describe('raven.Client', function() {
12121212
});
12131213
});
12141214

1215+
it('should captureBreadcrumb with processed exception', function(done) {
1216+
var calls = 0;
1217+
client = new raven.Client(dsn, {
1218+
shouldSendCallback: function(data) {
1219+
// Don't test first call, as there's no breadcrumbs there
1220+
if (calls === 0) {
1221+
calls += 1;
1222+
return false;
1223+
}
1224+
1225+
if (calls === 1) {
1226+
data.breadcrumbs.values.length.should.equal(1);
1227+
data.breadcrumbs.values[0].category.should.equal('sentry');
1228+
data.breadcrumbs.values[0].message.should.equal('Error: foo');
1229+
data.breadcrumbs.values[0].level.should.equal('error');
1230+
client.uninstall();
1231+
done();
1232+
}
1233+
}
1234+
});
1235+
1236+
client.install();
1237+
client.captureException(new Error('foo'));
1238+
client.captureException(new Error('bar'));
1239+
});
1240+
12151241
describe('#setContext', function() {
12161242
afterEach(function() {
12171243
process.domain && process.domain.exit();
@@ -1517,7 +1543,7 @@ describe('raven.Client', function() {
15171543
});
15181544
});
15191545

1520-
it('should not capture breadcrumbs for requests to sentry', function(done) {
1546+
it('should not capture breadcrumbs for requests to sentry, but should capture exception call itself', function(done) {
15211547
var scope = nock('https://app.getsentry.com')
15221548
.filteringRequestBody(/.*/, '*')
15231549
.post('/api/269/store/', '*')
@@ -1527,7 +1553,8 @@ describe('raven.Client', function() {
15271553
client.captureException(new Error('test'), function() {
15281554
// need to wait a tick because the response handler that captures the breadcrumb might run after this one
15291555
setTimeout(function() {
1530-
client.getContext().should.not.have.key('breadcrumbs');
1556+
client.getContext().breadcrumbs.length.should.equal(1);
1557+
client.getContext().breadcrumbs[0].category.should.equal('sentry');
15311558
scope.done();
15321559
done();
15331560
}, 0);

0 commit comments

Comments
 (0)