Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
Add shouldSendCallback, set*Callback methods
Browse files Browse the repository at this point in the history
  • Loading branch information
LewisJEllis committed Oct 25, 2016
1 parent 786970f commit ab8ec24
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
48 changes: 45 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extend(Raven.prototype, {

this.loggerName = options.logger || '';
this.dataCallback = options.dataCallback;
this.shouldSendCallback = options.shouldSendCallback;

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

if (this._enabled) {
var shouldSend = true;
if (!this._enabled) shouldSend = false;
if (this.shouldSendCallback && !this.shouldSendCallback()) shouldSend = false;

if (shouldSend) {
this.send(kwargs, cb);
} else {
setImmediate(function () {
// wish there was a good way to communicate to cb why we didn't send; worth considering cb api change?
// avoiding setImmediate here because node 0.8
setTimeout(function () {
cb(null, eventId);
});
}, 0);
}
},

Expand Down Expand Up @@ -310,6 +317,41 @@ extend(Raven.prototype, {
utils.consoleAlert('setTagsContext has been deprecated and will be removed in v2.0');
this._globalContext.tags = extend({}, this._globalContext.tags, tags);
return this;
},

setCallbackHelper: function (propertyName, callback) {
var original = this[propertyName];
if (typeof callback === 'function') {
this[propertyName] = function (data) {
return callback(data, original);
};
} else {
this[propertyName] = callback;
}

return this;
},

/*
* Set the dataCallback option
*
* @param {function} callback The callback to run which allows the
* data blob to be mutated before sending
* @return {Raven}
*/
setDataCallback: function (callback) {
return this.setCallbackHelper('dataCallback', callback);
},

/*
* Set the shouldSendCallback option
*
* @param {function} callback The callback to run which allows
* introspecting the blob before sending
* @return {Raven}
*/
setShouldSendCallback: function (callback) {
return this.setCallbackHelper('shouldSendCallback', callback);
}
});

Expand Down
18 changes: 18 additions & 0 deletions test/raven.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,24 @@ describe('raven.Client', function () {
});
});

it('should respect shouldSendCallback', function (done) {
client = new raven.Client(dsn, {
shouldSendCallback: function (data) {
return false;
}
});

// neither of these should fire, so report err to done if they do
client.on('logged', done);
client.on('error', done);

client.process({
message: 'test'
}, function (err, eventId) {
setTimeout(done, 10);
});
});

it('should call the callback after sending', function (done) {
var firedCallback = false;
var sentResponse = false;
Expand Down

0 comments on commit ab8ec24

Please sign in to comment.