Skip to content

Commit 5e7b3af

Browse files
committed
Extract _generateSyntheticStacktrace method from captureMessage
1 parent 270c55b commit 5e7b3af

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

src/raven.js

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -438,39 +438,7 @@ Raven.prototype = {
438438
);
439439

440440
if (this._globalOptions.stacktrace || (options && options.stacktrace)) {
441-
var ex;
442-
// Generate a "synthetic" stack trace from this point.
443-
// NOTE: If you are a Sentry user, and you are seeing this stack frame, it is NOT indicative
444-
// of a bug with Raven.js. Sentry generates synthetic traces either by configuration,
445-
// or if it catches a thrown object without a "stack" property.
446-
try {
447-
throw new Error(msg);
448-
} catch (ex1) {
449-
ex = ex1;
450-
}
451-
452-
// null exception name so `Error` isn't prefixed to msg
453-
ex.name = null;
454-
455-
options = objectMerge(
456-
{
457-
// fingerprint on msg, not stack trace (legacy behavior, could be
458-
// revisited)
459-
fingerprint: msg,
460-
// since we know this is a synthetic trace, the top N-most frames
461-
// MUST be from Raven.js, so mark them as in_app later by setting
462-
// trimHeadFrames
463-
trimHeadFrames: (options.trimHeadFrames || 0) + 1
464-
},
465-
options
466-
);
467-
468-
var stack = TraceKit.computeStackTrace(ex);
469-
var frames = this._prepareFrames(stack, options);
470-
data.stacktrace = {
471-
// Sentry expects frames oldest to newest
472-
frames: frames.reverse()
473-
};
441+
data.stacktrace = this._generateSyntheticStacktrace(msg, options);
474442
}
475443

476444
// Fire away!
@@ -1471,6 +1439,45 @@ Raven.prototype = {
14711439
this._send(data);
14721440
},
14731441

1442+
_generateSyntheticStacktrace: function(msg, options) {
1443+
options = options || {};
1444+
1445+
var ex;
1446+
// Generate a "synthetic" stack trace from this point.
1447+
// NOTE: If you are a Sentry user, and you are seeing this stack frame, it is NOT indicative
1448+
// of a bug with Raven.js. Sentry generates synthetic traces either by configuration,
1449+
// or if it catches a thrown object without a "stack" property.
1450+
try {
1451+
throw new Error(msg);
1452+
} catch (ex1) {
1453+
ex = ex1;
1454+
}
1455+
1456+
// null exception name so `Error` isn't prefixed to msg
1457+
ex.name = null;
1458+
1459+
options = objectMerge(
1460+
{
1461+
// fingerprint on msg, not stack trace (legacy behavior, could be
1462+
// revisited)
1463+
fingerprint: msg,
1464+
// since we know this is a synthetic trace, the top N-most frames
1465+
// MUST be from Raven.js, so mark them as in_app later by setting
1466+
// trimHeadFrames
1467+
trimHeadFrames: (options.trimHeadFrames || 0) + 1
1468+
},
1469+
options
1470+
);
1471+
1472+
var stack = TraceKit.computeStackTrace(ex);
1473+
var frames = this._prepareFrames(stack, options);
1474+
return {
1475+
// Sentry expects frames oldest to newest
1476+
// We also remove _generateSyntheticStacktrace call itself from the stack
1477+
frames: frames.slice(1).reverse()
1478+
};
1479+
},
1480+
14741481
_trimPacket: function(data) {
14751482
// For now, we only want to truncate the two different messages
14761483
// but this could/should be expanded to just trim everything

test/raven.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,15 @@ describe('globals', function() {
716716
});
717717
});
718718

719+
describe('generateSyntheticStacktrace', function() {
720+
it('should return an object with generated frames', function() {
721+
var stacktrace = Raven._generateSyntheticStacktrace('foo');
722+
723+
assert.isDefined(stacktrace.frames);
724+
assert.isAbove(stacktrace.frames.length, 1);
725+
});
726+
});
727+
719728
describe('send', function() {
720729
it('should build a good data payload', function() {
721730
this.sinon.stub(Raven, 'isSetup').returns(true);
@@ -2654,6 +2663,7 @@ describe('Raven (public API)', function() {
26542663
}
26552664

26562665
foo();
2666+
26572667
var frames = Raven._send.lastCall.args[0].stacktrace.frames;
26582668
assertSynthetic(frames);
26592669
});
@@ -2668,6 +2678,7 @@ describe('Raven (public API)', function() {
26682678
}
26692679

26702680
foo();
2681+
26712682
var frames = Raven._send.lastCall.args[0].stacktrace.frames;
26722683
assertSynthetic(frames);
26732684
});

0 commit comments

Comments
 (0)