diff --git a/lib/client.js b/lib/client.js index fde7b2d..8422ad0 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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'); @@ -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(); } @@ -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); }); @@ -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); } }); diff --git a/test/raven.client.js b/test/raven.client.js index 6aa548c..0219909 100644 --- a/test/raven.client.js +++ b/test/raven.client.js @@ -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'