Skip to content

Commit 31e58d3

Browse files
committed
feat: Allow to configure stacktrace for captureMessage calls
1 parent 5d8556a commit 31e58d3

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

docs/config.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ Those configuration options are documented below:
234234

235235
Controls how many requests can be maximally queued before bailing out and emitting an error. Defaults to `100`.
236236

237+
.. describe:: stacktrace
238+
239+
Attack stack trace to `captureMessage` calls by generatic "synthetic" error object and extracting all frames.
240+
237241
Environment Variables
238242
---------------------
239243

lib/client.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ extend(Raven.prototype, {
7070
this.sampleRate = typeof options.sampleRate === 'undefined' ? 1 : options.sampleRate;
7171
this.maxReqQueueCount = options.maxReqQueueCount || 100;
7272
this.parseUser = options.parseUser;
73+
this.stacktrace = options.stacktrace || false;
7374

7475
if (!this.dsn) {
7576
utils.consoleAlert('no DSN provided, error reporting disabled');
@@ -229,7 +230,7 @@ extend(Raven.prototype, {
229230
but also want to provide convenience for passing a req object and having us parse it out
230231
so we only parse a `req` property if the `request` property is absent/empty (and hence we won't clobber)
231232
parseUser returns a partial kwargs object with a `request` property and possibly a `user` property
232-
*/
233+
*/
233234
kwargs.request = this._createRequestObject(
234235
this._globalContext.request,
235236
domainContext.request,
@@ -318,8 +319,31 @@ extend(Raven.prototype, {
318319
} else {
319320
kwargs = kwargs || {};
320321
}
322+
321323
var eventId = this.generateEventId();
322-
this.process(eventId, parsers.parseText(message, kwargs), cb);
324+
325+
if (this.stacktrace) {
326+
var ex;
327+
// Generate a "synthetic" stack trace
328+
try {
329+
throw new Error(message);
330+
} catch (ex1) {
331+
ex = ex1;
332+
}
333+
334+
utils.parseStack(
335+
ex,
336+
function(frames) {
337+
// We trim last frame, as it's our `throw new Error(message)` call itself, which is redundant
338+
kwargs.stacktrace = {
339+
frames: frames.slice(0, -1)
340+
};
341+
this.process(eventId, parsers.parseText(message, kwargs), cb);
342+
}.bind(this)
343+
);
344+
} else {
345+
this.process(eventId, parsers.parseText(message, kwargs), cb);
346+
}
323347

324348
return eventId;
325349
},

test/raven.client.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@ describe('raven.Client', function() {
199199

200200
client.captureMessage('Hey!');
201201
});
202+
203+
it('should allow for attaching stacktrace', function(done) {
204+
var dsn = 'https://public:[email protected]:8443/269';
205+
var client = new raven.Client(dsn, {
206+
stacktrace: true
207+
});
208+
client.send = function mockSend(kwargs) {
209+
kwargs.message.should.equal('wtf?');
210+
kwargs.should.have.property('stacktrace');
211+
var stack = kwargs.stacktrace;
212+
stack.frames[stack.frames.length - 1].context_line.should.match(/captureMessage/);
213+
done();
214+
};
215+
client.captureMessage('wtf?');
216+
});
202217
});
203218

204219
describe('#captureException()', function() {

0 commit comments

Comments
 (0)