Skip to content

Commit b83f360

Browse files
committed
Add shouldSendCallback, set*Callback methods
1 parent 786970f commit b83f360

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

lib/client.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extend(Raven.prototype, {
4040

4141
this.loggerName = options.logger || '';
4242
this.dataCallback = options.dataCallback;
43+
this.shouldSendCallback = options.shouldSendCallback;
4344

4445
if (!this.dsn) {
4546
utils.consoleAlert('no DSN provided, error reporting disabled');
@@ -115,12 +116,18 @@ extend(Raven.prototype, {
115116
kwargs = this.dataCallback(kwargs);
116117
}
117118

118-
if (this._enabled) {
119+
var shouldSend = true;
120+
if (!this._enabled) shouldSend = false;
121+
if (this.shouldSendCallback && !this.shouldSendCallback()) shouldSend = false;
122+
123+
if (shouldSend) {
119124
this.send(kwargs, cb);
120125
} else {
121-
setImmediate(function () {
126+
// wish there was a good way to communicate to cb why we didn't send; worth considering cb api change?
127+
// avoiding setImmediate here because node 0.8
128+
setTimeout(function () {
122129
cb(null, eventId);
123-
});
130+
}, 0);
124131
}
125132
},
126133

@@ -310,6 +317,41 @@ extend(Raven.prototype, {
310317
utils.consoleAlert('setTagsContext has been deprecated and will be removed in v2.0');
311318
this._globalContext.tags = extend({}, this._globalContext.tags, tags);
312319
return this;
320+
},
321+
322+
setCallbackHelper: function (propertyName, callback) {
323+
var original = this[propertyName];
324+
if (typeof callback === 'function') {
325+
this[propertyName] = function (data) {
326+
return callback(data, original);
327+
};
328+
} else {
329+
this[propertyName] = callback;
330+
}
331+
332+
return this;
333+
},
334+
335+
/*
336+
* Set the dataCallback option
337+
*
338+
* @param {function} callback The callback to run which allows the
339+
* data blob to be mutated before sending
340+
* @return {Raven}
341+
*/
342+
setDataCallback: function (callback) {
343+
return this.setCallbackHelper('dataCallback', callback);
344+
},
345+
346+
/*
347+
* Set the shouldSendCallback option
348+
*
349+
* @param {function} callback The callback to run which allows
350+
* introspecting the blob before sending
351+
* @return {Raven}
352+
*/
353+
setShouldSendCallback: function (callback) {
354+
return this.setCallbackHelper('shouldSendCallback', callback);
313355
}
314356
});
315357

test/raven.client.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,24 @@ describe('raven.Client', function () {
382382
});
383383
});
384384

385+
it('should respect shouldSendCallback', function (done) {
386+
client = new raven.Client(dsn, {
387+
shouldSendCallback: function (data) {
388+
return false;
389+
}
390+
});
391+
392+
// neither of these should fire, so report err to done if they do
393+
client.on('logged', done);
394+
client.on('error', done);
395+
396+
client.process({
397+
message: 'test'
398+
}, function (err, eventId) {
399+
setTimeout(done, 10);
400+
});
401+
});
402+
385403
it('should call the callback after sending', function (done) {
386404
var firedCallback = false;
387405
var sentResponse = false;

0 commit comments

Comments
 (0)