Skip to content

Commit 4a8a6ff

Browse files
committed
Merge pull request #51 from MindscapeHQ/tags
Add setTags method
2 parents b26ac5f + e338c1e commit 4a8a6ff

File tree

4 files changed

+172
-119
lines changed

4 files changed

+172
-119
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ You can add tags to your error in the Send() function, as the fifth parameter. F
5959
client.send(new Error(), {}, function () {}, {}, ['custom tag 1', 'important error']);
6060
```
6161

62+
Tags can also be set globally using setTags
63+
64+
```javascript
65+
client.setTags(['Tag1', 'Tag2']);
66+
```
67+
6268
### Affected user tracking
6369

6470
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.
217223

218224
## Release History
219225

226+
- 0.8.2 - Add setTags method
220227
- 0.8.1 - Add custom error grouping key
221228
- 0.8.0 - Add offline support
222229
- 0.7.1 - Default useSSL to true

lib/raygun.js

Lines changed: 112 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -15,106 +15,122 @@ var MessageBuilder = require('./raygun.messageBuilder');
1515
var OfflineStorage = require('./raygun.offline');
1616

1717
var Raygun = function () {
18-
var _apiKey, _filters, raygun = this, _user, _version, _host, _port, _useSSL, _onBeforeSend, _offlineStorage, _isOffline, _offlineStorageOptions, _groupingKey;
19-
20-
raygun.init = function (options) {
21-
_apiKey = options.apiKey;
22-
_filters = options.filters;
23-
_host = options.host;
24-
_port = options.port;
25-
_useSSL = options.useSSL || true;
26-
_onBeforeSend = options.onBeforeSend;
27-
_offlineStorage = options.offlineStorage || new OfflineStorage();
28-
_offlineStorageOptions = options.offlineStorageOptions;
29-
_isOffline = options.isOffline;
30-
_groupingKey = options.groupingKey;
31-
32-
if(_isOffline) {
33-
_offlineStorage.init(_offlineStorageOptions);
34-
}
35-
36-
return raygun;
37-
};
38-
39-
raygun.user = function (req) {
40-
return;
41-
};
42-
43-
// This function is deprecated, is provided for legacy apps and will be
44-
// removed in 1.0: use raygun.user instead
45-
raygun.setUser = function (user) {
46-
_user = user;
47-
return raygun;
48-
};
49-
50-
raygun.setVersion = function (version) {
51-
_version = version;
52-
return raygun;
53-
};
54-
55-
raygun.onBeforeSend = function (onBeforeSend) {
56-
_onBeforeSend = onBeforeSend;
57-
return raygun;
58-
};
59-
60-
raygun.groupingKey = function (groupingKey) {
61-
_groupingKey = groupingKey;
62-
return raygun;
63-
};
64-
65-
raygun.offline = function() {
66-
_offlineStorage.init(_offlineStorageOptions);
67-
_isOffline = true;
68-
};
69-
70-
raygun.online = function(callback) {
71-
_isOffline = false;
72-
_offlineStorage.send(callback);
73-
};
74-
75-
raygun.send = function (exception, customData, callback, request, tags) {
76-
var builder = new MessageBuilder({ filters: _filters })
77-
.setErrorDetails(exception)
78-
.setRequestDetails(request)
79-
.setMachineName()
80-
.setEnvironmentDetails()
81-
.setUserCustomData(customData)
82-
.setUser(raygun.user(request) || _user)
83-
.setVersion(_version)
84-
.setTags(tags);
85-
86-
var message = builder.build();
87-
88-
if (_groupingKey) {
89-
message.details.groupingKey = typeof _groupingKey === 'function' ? _groupingKey(message, exception, customData, request, tags) : null;
90-
}
91-
92-
if (raygun.onBeforeSend) {
93-
message = typeof _onBeforeSend === 'function' ? _onBeforeSend(message, exception, customData, request, tags) : message;
94-
}
95-
96-
var transportMessage = {
97-
message: message,
98-
apiKey: _apiKey,
99-
callback: callback,
100-
host: _host,
101-
port: _port,
102-
useSSL: _useSSL
18+
var _apiKey, _filters, raygun = this, _user, _version, _host, _port, _useSSL, _onBeforeSend, _offlineStorage, _isOffline, _offlineStorageOptions, _groupingKey, _tags;
19+
20+
raygun.init = function (options) {
21+
_apiKey = options.apiKey;
22+
_filters = options.filters;
23+
_host = options.host;
24+
_port = options.port;
25+
_useSSL = options.useSSL || true;
26+
_onBeforeSend = options.onBeforeSend;
27+
_offlineStorage = options.offlineStorage || new OfflineStorage();
28+
_offlineStorageOptions = options.offlineStorageOptions;
29+
_isOffline = options.isOffline;
30+
_groupingKey = options.groupingKey;
31+
_tags = options.tags;
32+
33+
if (_isOffline) {
34+
_offlineStorage.init(_offlineStorageOptions);
35+
}
36+
37+
return raygun;
10338
};
10439

105-
if(_isOffline) {
106-
_offlineStorage.save(transportMessage, callback);
107-
} else {
108-
raygunTransport.send(transportMessage);
109-
}
40+
raygun.user = function (req) {
41+
return;
42+
};
11043

111-
return message;
112-
};
44+
// This function is deprecated, is provided for legacy apps and will be
45+
// removed in 1.0: use raygun.user instead
46+
raygun.setUser = function (user) {
47+
_user = user;
48+
return raygun;
49+
};
11350

114-
raygun.expressHandler = function (err, req, res, next) {
115-
raygun.send(err, {}, function () {}, req);
116-
next(err);
117-
};
51+
raygun.setVersion = function (version) {
52+
_version = version;
53+
return raygun;
54+
};
55+
56+
raygun.onBeforeSend = function (onBeforeSend) {
57+
_onBeforeSend = onBeforeSend;
58+
return raygun;
59+
};
60+
61+
raygun.groupingKey = function (groupingKey) {
62+
_groupingKey = groupingKey;
63+
return raygun;
64+
};
65+
66+
raygun.offline = function () {
67+
_offlineStorage.init(_offlineStorageOptions);
68+
_isOffline = true;
69+
};
70+
71+
raygun.online = function (callback) {
72+
_isOffline = false;
73+
_offlineStorage.send(callback);
74+
};
75+
76+
raygun.setTags = function (tags) {
77+
_tags = tags;
78+
};
79+
80+
raygun.send = function (exception, customData, callback, request, tags) {
81+
var mergedTags = [];
82+
83+
if (_tags) {
84+
mergedTags = mergedTags.concat(_tags);
85+
}
86+
87+
if (tags) {
88+
mergedTags = mergedTags.concat(tags);
89+
}
90+
91+
var builder = new MessageBuilder({filters: _filters})
92+
.setErrorDetails(exception)
93+
.setRequestDetails(request)
94+
.setMachineName()
95+
.setEnvironmentDetails()
96+
.setUserCustomData(customData)
97+
.setUser(raygun.user(request) || _user)
98+
.setVersion(_version)
99+
.setTags(mergedTags);
100+
101+
var message = builder.build();
102+
103+
if (_groupingKey) {
104+
message.details.groupingKey = typeof _groupingKey === 'function' ? _groupingKey(message, exception, customData, request, tags) : null;
105+
}
106+
107+
if (raygun.onBeforeSend) {
108+
message = typeof _onBeforeSend === 'function' ? _onBeforeSend(message, exception, customData, request, tags) : message;
109+
}
110+
111+
var transportMessage = {
112+
message: message,
113+
apiKey: _apiKey,
114+
callback: callback,
115+
host: _host,
116+
port: _port,
117+
useSSL: _useSSL
118+
};
119+
120+
if (_isOffline) {
121+
_offlineStorage.save(transportMessage, callback);
122+
} else {
123+
raygunTransport.send(transportMessage);
124+
}
125+
126+
return message;
127+
};
128+
129+
raygun.expressHandler = function (err, req, res, next) {
130+
raygun.send(err, {}, function () {
131+
}, req);
132+
next(err);
133+
};
118134
};
119135

120136
exports.Client = Raygun;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "raygun",
33
"description": "Raygun.io plugin for Node",
4-
"version": "0.8.1",
4+
"version": "0.8.2",
55
"homepage": "https://github.com/MindscapeHQ/raygun4node",
66
"author": {
77
"name": "MindscapeHQ",

test/raygun_send_test.js

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,63 @@ var Raygun = require('../lib/raygun.js');
66
// need to get these working, they time out for some reason, despite the call succeeding
77

88
test('send basic', {skip: true}, function (t) {
9-
t.plan(1);
10-
var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']});
11-
client.send(new Error(), {}, function (response) {
12-
t.equals(response.statusCode, 202);
13-
});
9+
t.plan(1);
10+
var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']});
11+
client.send(new Error(), {}, function (response) {
12+
t.equals(response.statusCode, 202);
13+
});
1414
});
1515

1616
test('send complex', {skip: true}, function (t) {
17-
t.plan(1);
18-
var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']}).setUser("callum@mindscape.co.nz").setVersion("1.0.0.0");
17+
t.plan(1);
18+
var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']}).setUser("callum@mindscape.co.nz").setVersion("1.0.0.0");
1919

20-
client.send(new Error(), {}, function (response) {
21-
t.equals(response.statusCode, 202);
22-
});
20+
client.send(new Error(), {}, function (response) {
21+
t.equals(response.statusCode, 202);
22+
});
2323
});
2424

2525
test('send with OnBeforeSend', {skip: true}, function (t) {
26-
t.plan(1);
27-
var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']});
28-
29-
var onBeforeSendCalled = false;
30-
client.onBeforeSend(function(payload){
31-
return payload;
32-
});
33-
34-
client.send(new Error(), {}, function () {
35-
t.equals(onBeforeSendCalled, true);
36-
t.end();
37-
});
26+
t.plan(1);
27+
var client = new Raygun.Client().init({apiKey: process.env['RAYGUN_APIKEY']});
28+
29+
var onBeforeSendCalled = false;
30+
client.onBeforeSend(function (payload) {
31+
return payload;
32+
});
33+
34+
client.send(new Error(), {}, function () {
35+
t.equals(onBeforeSendCalled, true);
36+
t.end();
37+
});
38+
});
39+
40+
test('check that tags get passed through', {}, function (t) {
41+
var tag = ['Test'];
42+
var client = new Raygun.Client().init({apiKey: 'TEST'});
43+
44+
client.setTags(tag);
45+
46+
client.onBeforeSend(function (payload) {
47+
t.same(payload.details.tags, tag);
48+
t.end();
49+
});
50+
51+
client.send(new Error(), {}, function () {
52+
t.end();
53+
});
54+
});
55+
56+
test('check that tags get merged', {}, function (t) {
57+
var client = new Raygun.Client().init({apiKey: 'TEST'});
58+
client.setTags(['Tag1']);
59+
60+
client.onBeforeSend(function (payload) {
61+
t.same(payload.details.tags, ['Tag1', 'Tag2']);
62+
t.end();
63+
});
64+
65+
client.send(new Error(), {}, function () {
66+
t.end();
67+
}, null, ["Tag2"]);
3868
});

0 commit comments

Comments
 (0)