forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·57 lines (44 loc) · 1.55 KB
/
Copy pathserver.js
File metadata and controls
executable file
·57 lines (44 loc) · 1.55 KB
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
#!/usr/bin/env node
const config = require('config');
const co = require('co');
const app = require('app');
const log = require('log')();
co(function*() {
yield* app.waitBootAndListen(config.server.host, config.server.port);
log.info("App is listening");
}).catch(function(err) {
log.error(err);
process.exit(1); // fatal error, could not boot!
});
// отслеживаем unhandled ошибки
// https://iojs.org/api/process.html#process_event_rejectionhandled
var unhandledRejections = [];
process.on('unhandledRejection', function(reason, p) {
p.trackRejectionId = Math.random();
setTimeout(function() { // 100 ms to catch up and handle rejection
if (p.trackRejectionId) { // if not rejectionHandled yet, report
unhandledRejections.push(p);
var report = {
err: reason,
trackRejectionId: p.trackRejectionId,
length: unhandledRejections.length
};
log.error(report, "unhandledRejection");
}
}, 100);
});
// если вдруг есть catch, но позже - скажем и об этом, с указанием промиса /trackRejectionId/
process.on('rejectionHandled', function(p) {
if (~unhandledRejections.indexOf(p)) {
// too more than 100 ms to handle
// already in the rejection list, let's report
unhandledRejections.splice(unhandledRejections.indexOf(p), 1);
log.error({
trackRejectionId: p.trackRejectionId,
length: unhandledRejections.length
}, "rejectionHandled");
} else {
// handled soon, don't track
delete p.trackRejectionId;
}
});