/
notify.js
151 lines (117 loc) · 3.63 KB
/
notify.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
var debug = require('debug')('axm:notify');
var util = require('util');
var Common = require('./common.js');
var Options = require('./configuration.js');
var Transport = require('./utils/transport.js');
var Notify = {};
var jsonize = function(err, filter, space) {
if (typeof(err) != 'object')
return err;
var plainObject = {};
Object.getOwnPropertyNames(err).forEach(function(key) {
plainObject[key] = err[key];
});
return plainObject;
};
Notify.catchAll = function(opts) {
var callsites = require('./utils/error-callsites')
if (opts === undefined)
opts = { errors : true };
Options.configureModule({
error : opts.errors
});
if (process.env.exec_mode === 'cluster_mode')
return false;
function getUncaughtExceptionListener(listener) {
return function uncaughtListener(err) {
var error = err && err.stack ? err.stack : err;
if (err && err.length) {
err._length = err.length;
delete err.length;
}
if (listener === 'unhandledRejection') {
error = 'You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:\n' + error;
}
console.error(error);
if (err)
var errObj = Notify._interpretError(err);
Transport.send({
type : 'process:exception',
data : errObj !== undefined ? errObj : {message: 'No error but ' + listener + ' was caught!' }
}, true);
if (!process.listeners(listener).filter(function (listener) {
return listener !== uncaughtListener;
}).length) {
if (listener == 'uncaughtException')
process.exit(1);
}
}
}
if (opts.errors === true && util.inspect(process.listeners('uncaughtException')).length === 2) {
process.once('uncaughtException', getUncaughtExceptionListener('uncaughtException'));
process.once('unhandledRejection', getUncaughtExceptionListener('unhandledRejection'));
}
else if (opts.errors === false
&& util.inspect(process.listeners('uncaughtException')).length !== 2) {
process.removeAllListeners('uncaughtException');
process.removeAllListeners('unhandledRejection');
}
};
Notify._interpretError = function(err) {
var s_err = {};
if (typeof(err) === 'string') {
// Simple string processing
s_err.message = err;
s_err.stack = err;
}
else if (!(err instanceof Error) && typeof(err) === 'object') {
// JSON processing
s_err.message = err;
s_err.stack = err;
}
else if (err instanceof Error) {
// Error object type processing
err.stack;
if (err.__error_callsites) {
var stackFrames = [];
err.__error_callsites.forEach(function(callSite) {
stackFrames.push({ file_name: callSite.getFileName(), line_number: callSite.getLineNumber()});
});
err.stackframes = stackFrames;
delete err.__error_callsites;
}
s_err = err;
}
return jsonize(s_err);
};
Notify.notify = function(err) {
var ret_err = this._interpretError(err);
// full_err
//debug(ret_err);
Transport.send({
type : 'process:exception',
data : ret_err
}, true);
return ret_err;
};
Notify.expressErrorHandler = function() {
var self = this;
Options.configureModule({
error : true
});
return function errorHandler(err, req, res, next) {
if (res.statusCode < 400) res.statusCode = 500;
//debug(err.stack || err);
err.url = req.url;
err.component = req.url;
err.action = req.method;
err.params = req.body;
err.session = req.session;
Transport.send({
type : 'process:exception',
data : jsonize(err)
}, true);
return next(err);
};
};
module.exports = Notify;