Skip to content

Commit

Permalink
Merge pull request #51 from MindscapeHQ/tags
Browse files Browse the repository at this point in the history
Add setTags method
  • Loading branch information
dwnz committed Dec 6, 2015
2 parents b26ac5f + e338c1e commit 4a8a6ff
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 119 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
208 changes: 112 additions & 96 deletions lib/raygun.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
74 changes: 52 additions & 22 deletions test/raygun_send_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]").setVersion("1.0.0.0");
t.plan(1);
var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']}).setUser("[email protected]").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"]);
});

0 comments on commit 4a8a6ff

Please sign in to comment.