Skip to content

Commit b1dbc89

Browse files
committed
Pass stacktrace via captureMessage as stacktrace interface
1 parent 407848e commit b1dbc89

File tree

3 files changed

+58
-41
lines changed

3 files changed

+58
-41
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ module.exports = function(grunt) {
131131
dest: 'build/raven.test.js',
132132
options: {
133133
browserifyOptions: {
134-
debug: true // source maps
134+
debug: false// source maps
135135
},
136136
plugin: [proxyquire.plugin]
137137
}

src/raven.js

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,18 @@ Raven.prototype = {
341341
* @return {Raven}
342342
*/
343343
captureMessage: function(msg, options) {
344-
if (options.stacktrace) {
344+
// config() automagically converts ignoreErrors from a list to a RegExp so we need to test for an
345+
// early call; we'll error on the side of logging anything called before configuration since it's
346+
// probably something you should see:
347+
if (!!this._globalOptions.ignoreErrors.test && this._globalOptions.ignoreErrors.test(msg)) {
348+
return;
349+
}
350+
351+
var data = objectMerge({
352+
message: msg + '' // Make sure it's actually a string
353+
}, options);
354+
355+
if (options && options.stacktrace) {
345356
var ex;
346357
// create a stack trace from this point; just trim
347358
// off extra frames so they don't include this function call (or
@@ -363,26 +374,16 @@ Raven.prototype = {
363374
trimTailFrames: (options.trimHeadFrames || 0) + 1
364375
}, options);
365376

366-
// infinite loop if ex *somehow* returns false for isError
367-
// is that possible when it is result of try/catch?
368-
this.captureException(ex, options);
369-
370-
return this;
371-
}
372-
373-
// config() automagically converts ignoreErrors from a list to a RegExp so we need to test for an
374-
// early call; we'll error on the side of logging anything called before configuration since it's
375-
// probably something you should see:
376-
if (!!this._globalOptions.ignoreErrors.test && this._globalOptions.ignoreErrors.test(msg)) {
377-
return;
377+
var stack = TraceKit.computeStackTrace(ex);
378+
var frames = this._buildNormalizedFrames(stack, options);
379+
data.stacktrace = {
380+
// Sentry expects frames oldest to newest
381+
frames: frames.reverse()
382+
}
378383
}
379384

380385
// Fire away!
381-
this._send(
382-
objectMerge({
383-
message: msg + '' // Make sure it's actually a string
384-
}, options)
385-
);
386+
this._send(data);
386387

387388
return this;
388389
},
@@ -1047,9 +1048,26 @@ Raven.prototype = {
10471048
},
10481049

10491050
_handleStackInfo: function(stackInfo, options) {
1051+
var frames = this._prepareFrames(stackInfo, options);
1052+
1053+
this._triggerEvent('handle', {
1054+
stackInfo: stackInfo,
1055+
options: options
1056+
});
1057+
1058+
this._processException(
1059+
stackInfo.name,
1060+
stackInfo.message,
1061+
stackInfo.url,
1062+
stackInfo.lineno,
1063+
frames,
1064+
options
1065+
);
1066+
},
1067+
1068+
_prepareFrames: function(stackInfo, options) {
10501069
var self = this;
10511070
var frames = [];
1052-
10531071
if (stackInfo.stack && stackInfo.stack.length) {
10541072
each(stackInfo.stack, function(i, stack) {
10551073
var frame = self._normalizeFrame(stack);
@@ -1059,30 +1077,25 @@ Raven.prototype = {
10591077
});
10601078

10611079
// e.g. frames captured via captureMessage throw
1062-
if (options.trimHeadFrames) {
1063-
frames = frames.slice(options.trimHeadFrames);
1080+
var j;
1081+
if (options && options.trimHeadFrames) {
1082+
for (j = 0; j < options.trimHeadFrames && j < frames.length; j++) {
1083+
frames[j].in_app = true;
1084+
}
10641085
}
1086+
10651087
// e.g. try/catch (wrapper) frames
1066-
if (options.trimTailFrames) {
1067-
frames = frames.slice(0, options.trimTailFrames);
1088+
if (options && options.trimTailFrames) {
1089+
for (j = options.trimTailFrames; j < frames.length; j++) {
1090+
frames[j].in_app = true;
1091+
}
10681092
}
10691093
}
1070-
1071-
this._triggerEvent('handle', {
1072-
stackInfo: stackInfo,
1073-
options: options
1074-
});
1075-
1076-
this._processException(
1077-
stackInfo.name,
1078-
stackInfo.message,
1079-
stackInfo.url,
1080-
stackInfo.lineno,
1081-
frames.slice(0, this._globalOptions.stackTraceLimit),
1082-
options
1083-
);
1094+
frames = frames.slice(0, this._globalOptions.stackTraceLimit);
1095+
return frames;
10841096
},
10851097

1098+
10861099
_normalizeFrame: function(frame) {
10871100
if (!frame.url) return;
10881101

@@ -1108,7 +1121,6 @@ Raven.prototype = {
11081121

11091122
_processException: function(type, message, fileurl, lineno, frames, options) {
11101123
var stacktrace;
1111-
11121124
if (!!this._globalOptions.ignoreErrors.test && this._globalOptions.ignoreErrors.test(message)) return;
11131125

11141126
message += '';

test/raven.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,7 +1653,10 @@ describe('Raven (public API)', function() {
16531653
Raven.context({foo: 'bar'}, broken);
16541654
}, error);
16551655
assert.isTrue(Raven.captureException.called);
1656-
assert.deepEqual(Raven.captureException.lastCall.args, [error, {'foo': 'bar'}]);
1656+
assert.deepEqual(Raven.captureException.lastCall.args, [error, {
1657+
'foo': 'bar',
1658+
trimTailFrames: 1 // because wrap
1659+
}]);
16571660
});
16581661

16591662
it('should capture the exception without options', function() {
@@ -1664,7 +1667,9 @@ describe('Raven (public API)', function() {
16641667
Raven.context(broken);
16651668
}, error);
16661669
assert.isTrue(Raven.captureException.called);
1667-
assert.deepEqual(Raven.captureException.lastCall.args, [error, undefined]);
1670+
assert.deepEqual(Raven.captureException.lastCall.args, [error, {
1671+
trimTailFrames: 1 // because wrap
1672+
}]);
16681673
});
16691674

16701675
it('should execute the callback without arguments', function() {

0 commit comments

Comments
 (0)