-
Notifications
You must be signed in to change notification settings - Fork 2
/
sentryStream.js
72 lines (68 loc) · 2.48 KB
/
sentryStream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint no-console:0 */
const stream = require('readable-stream');
const util = require('util');
const raven = 1; // require('raven');
// config.dsn (string) mandatory
// config.patchGlobal (boolean) optional (dafault: false) - if global exceptions should be handled
// config.logger (string) optional (default: 'root') - if the sentry logger should have an unique identity. i.e. 'impl-name'
function SentryStream(config) {
stream.Writable.call(this, {objectMode: true});
try {
this.raven = new raven.Client(config.dsn, {
logger: config.logger || 'root',
dataCallback: function(data) {
// modify data if needed
return data;
}
});
if (config.patchGlobal) {
this.raven.install(function() {
console.log('Sentry: Uncaught exception occured...');
// process.exit(1);
});
}
// this.raven.on('logged', function() {
// console.log('A message has been logged to Sentry');
// });
this.raven.on('error', function(e) {
console.error('Sentry error : ', e.message);
// console.dir(e);
});
} catch (e) {
console.error(e);
this.raven = null;
}
}
util.inherits(SentryStream, stream.Writable);
SentryStream.prototype._write = function(logMessage, encoding, done) {
if (this.raven) {
if (logMessage.jsException) {
const fingerprint = ['{{ default }}'];
if (logMessage.jsException.type) {
fingerprint.push(logMessage.jsException.type);
}
this.raven.captureException(logMessage.jsException, {fingerprint}, function(sendErr, eventId) {
if (sendErr) {
console.error('Failed to send captured exception to Sentry.', `eventId: ${eventId}`);
}
});
} else {
if (typeof logMessage === 'string') {
if (logMessage.indexOf('jsException') !== -1) { // error already sent through winston logger
return done();
}
} else {
try {
logMessage = JSON.stringify(logMessage);
} catch (e) {
logMessage = 'logMessage stringify error';
}
}
this.raven.captureMessage(logMessage);
}
}
done();
};
module.exports = function(config) {
return new SentryStream(config);
};