meshuser: log uncaught exceptions to mesherrs

Noah Zalev committed Jul 10, 2021 at 20:14 UTC ad6ff11413e73ffd63b94943894b8038a4827a15
2 files changed +12 -6
meshcentral.js
+9 -5
@@ -495,15 +495,19 @@ function CreateMeshCentralServer(config, args) {
495 console.log('ERR: ' + datastr);
496 if (data.startsWith('le.challenges[tls-sni-01].loopback')) { return; } // Ignore this error output from GreenLock
497 if (data[data.length - 1] == '\n') { data = data.substring(0, data.length - 1); }
498 - try {
499 - var errlogpath = null;
500 - if (typeof obj.args.mesherrorlogpath == 'string') { errlogpath = obj.path.join(obj.args.mesherrorlogpath, 'mesherrors.txt'); } else { errlogpath = obj.getConfigFilePath('mesherrors.txt'); }
501 - obj.fs.appendFileSync(errlogpath, '-------- ' + new Date().toLocaleString() + ' ---- ' + getCurrentVersion() + ' --------\r\n\r\n' + data + '\r\n\r\n\r\n');
502 - } catch (ex) { console.log('ERROR: Unable to write to mesherrors.txt.'); }
498 + obj.logError(data);
499 });
500 childProcess.on('close', function (code) { if ((code != 0) && (code != 123)) { /* console.log("Exited with code " + code); */ } });
501 };
502
503 + obj.logError = function (err) {
504 + try {
505 + var errlogpath = null;
506 + if (typeof obj.args.mesherrorlogpath == 'string') { errlogpath = obj.path.join(obj.args.mesherrorlogpath, 'mesherrors.txt'); } else { errlogpath = obj.getConfigFilePath('mesherrors.txt'); }
507 + obj.fs.appendFileSync(errlogpath, '-------- ' + new Date().toLocaleString() + ' ---- ' + getCurrentVersion() + ' --------\r\n\r\n' + err + '\r\n\r\n\r\n');
508 + } catch (ex) { console.log('ERROR: Unable to write to mesherrors.txt.'); }
509 + };
510 +
511 // Get current and latest MeshCentral server versions using NPM
512 obj.getLatestServerVersion = function (callback) {
513 if (callback == null) return;
meshuser.js
+3 -1
@@ -555,7 +555,9 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
555 if (commandHandler != null) {
556 try { commandHandler(command); return;
557 } catch (e) {
558 - console.log('Unhandled error while processing ' + command.action + ' for user ' + + user.name + ':\n' + e);
558 + console.log('Unhandled error while processing ' + command.action + ' for user ' + user.name + ':\n' + e);
559 + parent.parent.logError(e.stack);
560 + return;
561 }
562 } else {
563 // console.log('Unknown action from user ' + user.name + ': ' + command.action + '.');