From ab8ec2430a5dcaf3d92428485809d3754ad06dc9 Mon Sep 17 00:00:00 2001 From: Lewis Ellis Date: Mon, 24 Oct 2016 14:06:26 -0700 Subject: [PATCH] Add shouldSendCallback, set*Callback methods --- lib/client.js | 48 +++++++++++++++++++++++++++++++++++++++++--- test/raven.client.js | 18 +++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/lib/client.js b/lib/client.js index 1401383..0946fda 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'); @@ -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); } }, @@ -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); } }); diff --git a/test/raven.client.js b/test/raven.client.js index 27bd0aa..9f38374 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 call the callback after sending', function (done) { var firedCallback = false; var sentResponse = false;