diff --git a/README.md b/README.md index 16c5e2e..651bcf9 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,12 @@ You can add tags to your error in the Send() function, as the fifth parameter. F client.send(new Error(), {}, function () {}, {}, ['custom tag 1', 'important error']); ``` +Tags can also be set globally using setTags + +```javascript +client.setTags(['Tag1', 'Tag2']); +``` + ### Affected user tracking New in 0.4: You can set **raygunClient.user** to a function that returns the user name or email address of the currently logged in user. @@ -217,6 +223,7 @@ In lieu of a formal styleguide, take care to maintain the existing coding style. ## Release History +- 0.8.2 - Add setTags method - 0.8.1 - Add custom error grouping key - 0.8.0 - Add offline support - 0.7.1 - Default useSSL to true diff --git a/lib/raygun.js b/lib/raygun.js index ba59862..520a135 100644 --- a/lib/raygun.js +++ b/lib/raygun.js @@ -15,106 +15,122 @@ var MessageBuilder = require('./raygun.messageBuilder'); var OfflineStorage = require('./raygun.offline'); var Raygun = function () { - var _apiKey, _filters, raygun = this, _user, _version, _host, _port, _useSSL, _onBeforeSend, _offlineStorage, _isOffline, _offlineStorageOptions, _groupingKey; - - raygun.init = function (options) { - _apiKey = options.apiKey; - _filters = options.filters; - _host = options.host; - _port = options.port; - _useSSL = options.useSSL || true; - _onBeforeSend = options.onBeforeSend; - _offlineStorage = options.offlineStorage || new OfflineStorage(); - _offlineStorageOptions = options.offlineStorageOptions; - _isOffline = options.isOffline; - _groupingKey = options.groupingKey; - - if(_isOffline) { - _offlineStorage.init(_offlineStorageOptions); - } - - return raygun; - }; - - raygun.user = function (req) { - return; - }; - - // This function is deprecated, is provided for legacy apps and will be - // removed in 1.0: use raygun.user instead - raygun.setUser = function (user) { - _user = user; - return raygun; - }; - - raygun.setVersion = function (version) { - _version = version; - return raygun; - }; - - raygun.onBeforeSend = function (onBeforeSend) { - _onBeforeSend = onBeforeSend; - return raygun; - }; - - raygun.groupingKey = function (groupingKey) { - _groupingKey = groupingKey; - return raygun; - }; - - raygun.offline = function() { - _offlineStorage.init(_offlineStorageOptions); - _isOffline = true; - }; - - raygun.online = function(callback) { - _isOffline = false; - _offlineStorage.send(callback); - }; - - raygun.send = function (exception, customData, callback, request, tags) { - var builder = new MessageBuilder({ filters: _filters }) - .setErrorDetails(exception) - .setRequestDetails(request) - .setMachineName() - .setEnvironmentDetails() - .setUserCustomData(customData) - .setUser(raygun.user(request) || _user) - .setVersion(_version) - .setTags(tags); - - var message = builder.build(); - - if (_groupingKey) { - message.details.groupingKey = typeof _groupingKey === 'function' ? _groupingKey(message, exception, customData, request, tags) : null; - } - - if (raygun.onBeforeSend) { - message = typeof _onBeforeSend === 'function' ? _onBeforeSend(message, exception, customData, request, tags) : message; - } - - var transportMessage = { - message: message, - apiKey: _apiKey, - callback: callback, - host: _host, - port: _port, - useSSL: _useSSL + var _apiKey, _filters, raygun = this, _user, _version, _host, _port, _useSSL, _onBeforeSend, _offlineStorage, _isOffline, _offlineStorageOptions, _groupingKey, _tags; + + raygun.init = function (options) { + _apiKey = options.apiKey; + _filters = options.filters; + _host = options.host; + _port = options.port; + _useSSL = options.useSSL || true; + _onBeforeSend = options.onBeforeSend; + _offlineStorage = options.offlineStorage || new OfflineStorage(); + _offlineStorageOptions = options.offlineStorageOptions; + _isOffline = options.isOffline; + _groupingKey = options.groupingKey; + _tags = options.tags; + + if (_isOffline) { + _offlineStorage.init(_offlineStorageOptions); + } + + return raygun; }; - if(_isOffline) { - _offlineStorage.save(transportMessage, callback); - } else { - raygunTransport.send(transportMessage); - } + raygun.user = function (req) { + return; + }; - return message; - }; + // This function is deprecated, is provided for legacy apps and will be + // removed in 1.0: use raygun.user instead + raygun.setUser = function (user) { + _user = user; + return raygun; + }; - raygun.expressHandler = function (err, req, res, next) { - raygun.send(err, {}, function () {}, req); - next(err); - }; + raygun.setVersion = function (version) { + _version = version; + return raygun; + }; + + raygun.onBeforeSend = function (onBeforeSend) { + _onBeforeSend = onBeforeSend; + return raygun; + }; + + raygun.groupingKey = function (groupingKey) { + _groupingKey = groupingKey; + return raygun; + }; + + raygun.offline = function () { + _offlineStorage.init(_offlineStorageOptions); + _isOffline = true; + }; + + raygun.online = function (callback) { + _isOffline = false; + _offlineStorage.send(callback); + }; + + raygun.setTags = function (tags) { + _tags = tags; + }; + + raygun.send = function (exception, customData, callback, request, tags) { + var mergedTags = []; + + if (_tags) { + mergedTags = mergedTags.concat(_tags); + } + + if (tags) { + mergedTags = mergedTags.concat(tags); + } + + var builder = new MessageBuilder({filters: _filters}) + .setErrorDetails(exception) + .setRequestDetails(request) + .setMachineName() + .setEnvironmentDetails() + .setUserCustomData(customData) + .setUser(raygun.user(request) || _user) + .setVersion(_version) + .setTags(mergedTags); + + var message = builder.build(); + + if (_groupingKey) { + message.details.groupingKey = typeof _groupingKey === 'function' ? _groupingKey(message, exception, customData, request, tags) : null; + } + + if (raygun.onBeforeSend) { + message = typeof _onBeforeSend === 'function' ? _onBeforeSend(message, exception, customData, request, tags) : message; + } + + var transportMessage = { + message: message, + apiKey: _apiKey, + callback: callback, + host: _host, + port: _port, + useSSL: _useSSL + }; + + if (_isOffline) { + _offlineStorage.save(transportMessage, callback); + } else { + raygunTransport.send(transportMessage); + } + + return message; + }; + + raygun.expressHandler = function (err, req, res, next) { + raygun.send(err, {}, function () { + }, req); + next(err); + }; }; exports.Client = Raygun; diff --git a/package.json b/package.json index 6e3a389..b214939 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "raygun", "description": "Raygun.io plugin for Node", - "version": "0.8.1", + "version": "0.8.2", "homepage": "https://github.com/MindscapeHQ/raygun4node", "author": { "name": "MindscapeHQ", diff --git a/test/raygun_send_test.js b/test/raygun_send_test.js index 5479644..c3e58b1 100644 --- a/test/raygun_send_test.js +++ b/test/raygun_send_test.js @@ -6,33 +6,63 @@ var Raygun = require('../lib/raygun.js'); // need to get these working, they time out for some reason, despite the call succeeding test('send basic', {skip: true}, function (t) { - t.plan(1); - var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']}); - client.send(new Error(), {}, function (response) { - t.equals(response.statusCode, 202); - }); + t.plan(1); + var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']}); + client.send(new Error(), {}, function (response) { + t.equals(response.statusCode, 202); + }); }); test('send complex', {skip: true}, function (t) { - t.plan(1); - var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']}).setUser("callum@mindscape.co.nz").setVersion("1.0.0.0"); + t.plan(1); + var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']}).setUser("callum@mindscape.co.nz").setVersion("1.0.0.0"); - client.send(new Error(), {}, function (response) { - t.equals(response.statusCode, 202); - }); + client.send(new Error(), {}, function (response) { + t.equals(response.statusCode, 202); + }); }); test('send with OnBeforeSend', {skip: true}, function (t) { - t.plan(1); - var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']}); - - var onBeforeSendCalled = false; - client.onBeforeSend(function(payload){ - return payload; - }); - - client.send(new Error(), {}, function () { - t.equals(onBeforeSendCalled, true); - t.end(); - }); + t.plan(1); + var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']}); + + var onBeforeSendCalled = false; + client.onBeforeSend(function (payload) { + return payload; + }); + + client.send(new Error(), {}, function () { + t.equals(onBeforeSendCalled, true); + t.end(); + }); +}); + +test('check that tags get passed through', {}, function (t) { + var tag = ['Test']; + var client = new Raygun.Client().init({apiKey: 'TEST'}); + + client.setTags(tag); + + client.onBeforeSend(function (payload) { + t.same(payload.details.tags, tag); + t.end(); + }); + + client.send(new Error(), {}, function () { + t.end(); + }); +}); + +test('check that tags get merged', {}, function (t) { + var client = new Raygun.Client().init({apiKey: 'TEST'}); + client.setTags(['Tag1']); + + client.onBeforeSend(function (payload) { + t.same(payload.details.tags, ['Tag1', 'Tag2']); + t.end(); + }); + + client.send(new Error(), {}, function () { + t.end(); + }, null, ["Tag2"]); }); \ No newline at end of file