Skip to content

Commit a355140

Browse files
committed
Move handleUncaught into this project
1 parent 51b3fd6 commit a355140

File tree

4 files changed

+77
-11
lines changed

4 files changed

+77
-11
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
"react-with-direction": "^1.3.1",
7171
"redux": "^4.0.5",
7272
"redux-logger": "^3.0.6",
73-
"rethink-handle-uncaught": "^1.0.0",
7473
"rethinkdbdash": "^2.3.31",
7574
"socket.io": "^2.3.0",
7675
"socket.io-client": "^2.3.1",

src/common/db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"use strict";
33
const options = require("../../config/database.js");
44
const r = (module.exports = require("rethinkdbdash")(options));
5-
require("rethink-handle-uncaught")(r, {
5+
require("./handleUncaught")(r, {
66
filter: function(errorOrException) {
77
if (errorOrException.msg === "Malicious Path") {
88
return false;

src/common/handleUncaught.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
const os = require('os');
3+
4+
module.exports = function(r, opts) {
5+
opts = opts || {
6+
table: 'errors'
7+
};
8+
9+
if (!opts.filter) {
10+
opts.filter = () => true;
11+
}
12+
13+
if (! typeof opts.filter === 'function') {
14+
throw new TypeError('opts.filter must be a function');
15+
}
16+
if (! typeof opts.table === 'string') {
17+
throw new TypeError('opts.table must be a string');
18+
}
19+
20+
function getStack(errOrPromise) {
21+
if (errOrPromise == null) {
22+
return null;
23+
} else if (errOrPromise.stack != null) {
24+
return errOrPromise.stack;
25+
} else {
26+
return errOrPromise;
27+
}
28+
}
29+
30+
function insert(errOrPromise, isPromise) {
31+
var firstStep;
32+
33+
if (opts.filter(errOrPromise)) {
34+
firstStep = r.table(opts.table).insert({
35+
host: os.hostname(),
36+
pid: process.pid,
37+
date: new Date(),
38+
stack: getStack(errOrPromise),
39+
argv: process.argv,
40+
cwd: process.cwd(),
41+
memory: process.memoryUsage(),
42+
uptime: process.uptime(),
43+
promise: isPromise,
44+
uid: process.getuid(),
45+
groups: process.getgroups(),
46+
load: os.loadavg()
47+
48+
}).run()
49+
} else {
50+
firstStep = Promise.resolve();
51+
}
52+
firstStep
53+
.catch(function (err) {
54+
// If the DB is unavailable, we would go in an infinite loop trying to keep inserting exceptions.
55+
console.error(getStack(err));
56+
})
57+
58+
return firstStep;
59+
}
60+
61+
function handleUncaughtException(err) {
62+
console.error((new Date).toUTCString() + ' uncaughtException:', getStack(err))
63+
return insert(err, false)
64+
.finally(function() {
65+
process.exit(1)
66+
})
67+
};
68+
69+
function handleUnhandledRejection(reason, promise) {
70+
console.error((new Date).toUTCString() + ' unhandledPromiseRejection:', getStack(reason))
71+
return insert(reason, true);
72+
};
73+
74+
process.on('uncaughtException', handleUncaughtException);
75+
process.on('unhandledRejection', handleUnhandledRejection);
76+
}

src/common/systemdb.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,3 @@ const db = require("rethinkdbdash")({
88
db: 'rethinkdb',
99
});
1010
module.exports.db = db;
11-
12-
// require("rethink-handle-uncaught")(r, {
13-
// filter: function(errorOrException) {
14-
// if (errorOrException.msg === "Malicious Path") {
15-
// return false;
16-
// }
17-
// return true;
18-
// }
19-
// });

0 commit comments

Comments
 (0)