-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
169 lines (154 loc) · 5.08 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var https = require('https');
var stackTrace = require('stack-trace');
var os = require('os');
var formatFrames = function (frames, project_root) {
var result = [];
for (var i in frames) {
var frame = frames[i];
if (project_root && frame.fileName && frame.fileName.lastIndexOf(project_root, 0) === 0 && frame.fileName.indexOf("node_modules") === -1) {
result.push([frame.fileName, frame.lineNumber + ':' + frame.columnNumber, frame.functionName, {"in-app" : true}]);
} else {
result.push([frame.fileName, frame.lineNumber + ':' + frame.columnNumber, frame.functionName]);
}
}
return result;
};
var formatError = function (e, options, project_root) {
var opts = options || {};
var stacktrace = stackTrace.parse(e);
return {
stacktrace: formatFrames(stacktrace, project_root),
type: e.name || 'Error',
message: e.message,
'custom-data': opts.customData,
url: opts.url,
location: opts.location,
host: opts.host,
};
};
var DEFAULT_ENDPOINTS = [
'collector1.yellerapp.com',
'collector2.yellerapp.com',
'collector3.yellerapp.com',
'collector4.yellerapp.com',
'collector5.yellerapp.com',
];
var VERSION = "yeller_node: 0.0.7";
var YellerClient = function (options) {
this.token = options.token;
this.endpoints = options.endpoints;
this.maxRetryCount = this.endpoints.length * 2;
this.errorHandler = options.errorHandler;
this.startupOptions = {
applicationEnvironment: options.applicationEnvironment,
host: options.host,
};
this.developmentEnvironments = options.developmentEnvironments;
this.project_root = options.project_root || process.cwd();
};
YellerClient.prototype.rotateEndpoint = function () {
var lastEndpoint = this.endpoints.shift();
this.endpoints.push(lastEndpoint);
};
YellerClient.prototype.formatJSONError = function (error, options) {
var formatted = formatError(error, options, this.project_root);
formatted['application-environment'] = this.startupOptions.applicationEnvironment;
formatted.host = this.startupOptions.host;
formatted['client-version'] = VERSION;
return JSON.stringify(formatted);
};
YellerClient.prototype.handleFailure = function (jsonError, currentRequestCount, callback, error) {
if (currentRequestCount < this.maxRetryCount) {
this.reportAndHandleRetries(jsonError, currentRequestCount + 1, callback);
} else {
callback(error);
this.errorHandler.ioError(error);
}
};
YellerClient.prototype.reportAndHandleRetries = function (jsonError, currentRequestCount, callback) {
var that = this;
var yellerCallback = function (res) {
that.rotateEndpoint();
if (res.statusCode === 200) {
callback();
} else if (res.statusCode >= 400 && res.statusCode < 500) {
that.errorHandler.authError(res);
callback();
} else {
that.handleFailure(jsonError, currentRequestCount, callback, res);
}
};
var req = https.request({
host: this.endpoints[0],
path: '/' + this.token,
method: 'POST',
headers: {
'Content-Type' : 'application/json',
'Content-Length' : jsonError.length
},
},
yellerCallback);
req.write(jsonError);
req.setTimeout(10000, function (err) {
if (err === undefined) {
that.handleFailure(jsonError, currentRequestCount, callback, new Error('Yeller: timed out sending the error to servers'));
} else {
that.handleFailure(jsonError, currentRequestCount, callback, err);
}
});
req.on('error', function (err) {
that.handleFailure(jsonError, currentRequestCount, callback, err);
});
req.end();
};
YellerClient.prototype.report = function(error, opts, call) {
var options = opts || options;
var callback = call || function() {};
var json = this.formatJSONError(error, options);
this.reportAndHandleRetries(json, 1, callback);
};
var YellerIgnoringClient = function () {
};
YellerIgnoringClient.prototype.report = function (e, opts, callback) {
if (typeof callback === 'function') {
callback();
}
};
var client = function(opts) {
if (!opts.endpoints) {
opts.endpoints = DEFAULT_ENDPOINTS.slice(0);
}
if (!opts.applicationEnvironment) {
opts.applicationEnvironment = 'production';
}
if (!opts.host) {
opts.host = os.hostname();
}
if (!opts.errorHandler) {
opts.errorHandler = {
ioError: function (err) {
console.log("Yeller encountered an IO error: ", err);
console.log(err.stack);
},
authError: function (err) {
console.log("Yeller encountered an Authorization error: ", err, " please check your api key is valid and that you've got an active Yeller account");
console.log(err.stack);
},
};
}
if (!opts.developmentEnvironments) {
opts.developmentEnvironments = ['development', 'test'];
}
for (var i in opts.developmentEnvironments) {
var env = opts.developmentEnvironments[i];
if (env === opts.applicationEnvironment) {
return new YellerIgnoringClient();
}
}
return new YellerClient(opts);
};
module.exports = {
client: client,
formatError: formatError,
formatFrames: formatFrames,
};