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 24, 2016
1 parent 03b5e1e commit 7c1ee43
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
44 changes: 43 additions & 1 deletion 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 @@ -80,6 +81,7 @@ extend(Raven.prototype, {
process: function process(eventId, kwargs, cb) {
// prod codepaths shouldn't hit this branch, for testing
if (typeof eventId === 'object') {
cb = kwargs;
kwargs = eventId;
eventId = this.generateEventId();
}
Expand Down Expand Up @@ -114,9 +116,14 @@ 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 {
// wish there was a good way to communicate to cb why we didn't send; worth considering cb api change?
setImmediate(function () {
cb(null, eventId);
});
Expand Down Expand Up @@ -309,6 +316,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 attach environment', function (done) {
client = new raven.Client(dsn, {
environment: 'staging'
Expand Down

0 comments on commit 7c1ee43

Please sign in to comment.