Skip to content

Commit 617c051

Browse files
committed
Pass stacktrace via captureMessage as stacktrace interface
1 parent 4236f39 commit 617c051

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
},
@@ -1053,9 +1054,26 @@ Raven.prototype = {
10531054
},
10541055

10551056
_handleStackInfo: function(stackInfo, options) {
1057+
var frames = this._prepareFrames(stackInfo, options);
1058+
1059+
this._triggerEvent('handle', {
1060+
stackInfo: stackInfo,
1061+
options: options
1062+
});
1063+
1064+
this._processException(
1065+
stackInfo.name,
1066+
stackInfo.message,
1067+
stackInfo.url,
1068+
stackInfo.lineno,
1069+
frames,
1070+
options
1071+
);
1072+
},
1073+
1074+
_prepareFrames: function(stackInfo, options) {
10561075
var self = this;
10571076
var frames = [];
1058-
10591077
if (stackInfo.stack && stackInfo.stack.length) {
10601078
each(stackInfo.stack, function(i, stack) {
10611079
var frame = self._normalizeFrame(stack);
@@ -1065,30 +1083,25 @@ Raven.prototype = {
10651083
});
10661084

10671085
// e.g. frames captured via captureMessage throw
1068-
if (options.trimHeadFrames) {
1069-
frames = frames.slice(options.trimHeadFrames);
1086+
var j;
1087+
if (options && options.trimHeadFrames) {
1088+
for (j = 0; j < options.trimHeadFrames && j < frames.length; j++) {
1089+
frames[j].in_app = true;
1090+
}
10701091
}
1092+
10711093
// e.g. try/catch (wrapper) frames
1072-
if (options.trimTailFrames) {
1073-
frames = frames.slice(0, options.trimTailFrames);
1094+
if (options && options.trimTailFrames) {
1095+
for (j = options.trimTailFrames; j < frames.length; j++) {
1096+
frames[j].in_app = true;
1097+
}
10741098
}
10751099
}
1076-
1077-
this._triggerEvent('handle', {
1078-
stackInfo: stackInfo,
1079-
options: options
1080-
});
1081-
1082-
this._processException(
1083-
stackInfo.name,
1084-
stackInfo.message,
1085-
stackInfo.url,
1086-
stackInfo.lineno,
1087-
frames.slice(0, this._globalOptions.stackTraceLimit),
1088-
options
1089-
);
1100+
frames = frames.slice(0, this._globalOptions.stackTraceLimit);
1101+
return frames;
10901102
},
10911103

1104+
10921105
_normalizeFrame: function(frame) {
10931106
if (!frame.url) return;
10941107

@@ -1114,7 +1127,6 @@ Raven.prototype = {
11141127

11151128
_processException: function(type, message, fileurl, lineno, frames, options) {
11161129
var stacktrace;
1117-
11181130
if (!!this._globalOptions.ignoreErrors.test && this._globalOptions.ignoreErrors.test(message)) return;
11191131

11201132
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)