logging JS errors to backend logs

This commit is contained in:
azivner 2017-12-01 22:28:22 -05:00
parent 02e5d20d44
commit cba9d8b5c1
6 changed files with 59 additions and 11 deletions

View file

@ -121,7 +121,7 @@ const contextMenu = (function() {
treeChanges.deleteNode(node); treeChanges.deleteNode(node);
} }
else { else {
console.log("Unknown command: " + ui.cmd); messaging.logError("Unknown command: " + ui.cmd);
} }
} }
}; };

View file

@ -80,7 +80,7 @@ const jumpToNote = (function() {
dialogEl.dialog("close"); dialogEl.dialog("close");
} }
else { else {
console.error("Unknown action=" + action); messaging.logError("Unknown action=" + action);
} }
return false; return false;

View file

@ -126,3 +126,26 @@ function showAppIfHidden() {
loaderDiv.style.opacity = 0.0; loaderDiv.style.opacity = 0.0;
} }
} }
window.onerror = function (msg, url, lineNo, columnNo, error) {
const string = msg.toLowerCase();
let message = "Uncaught error: ";
if (string.indexOf("script error") > -1){
message += 'No details available';
}
else {
message += [
'Message: ' + msg,
'URL: ' + url,
'Line: ' + lineNo,
'Column: ' + columnNo,
'Error object: ' + JSON.stringify(error)
].join(' - ');
}
messaging.logError(message);
return false;
};

View file

@ -1,9 +1,20 @@
"use strict"; "use strict";
const messaging = (function() { const messaging = (function() {
function messageHandler(event) { let ws = null;
console.log(event.data);
function logError(message) {
console.error(message);
if (ws && ws.readyState === 1) {
ws.send(JSON.stringify({
type: 'log-error',
error: message
}));
}
}
function messageHandler(event) {
const message = JSON.parse(event.data); const message = JSON.parse(event.data);
if (message.type === 'sync') { if (message.type === 'sync') {
@ -27,8 +38,6 @@ const messaging = (function() {
} }
} }
let ws = null;
function connectWebSocket() { function connectWebSocket() {
// use wss for secure messaging // use wss for secure messaging
ws = new WebSocket("ws://" + location.host); ws = new WebSocket("ws://" + location.host);
@ -65,4 +74,8 @@ const messaging = (function() {
showMessage("Re-connected to server"); showMessage("Re-connected to server");
} }
}, 3000); }, 3000);
return {
logError
};
})(); })();

View file

@ -144,7 +144,7 @@ const noteTree = (function() {
function prepareNoteTreeInner(parentNoteId) { function prepareNoteTreeInner(parentNoteId) {
const childNoteIds = parentToChildren[parentNoteId]; const childNoteIds = parentToChildren[parentNoteId];
if (!childNoteIds) { if (!childNoteIds) {
console.log("No children for " + parentNoteId + ". This shouldn't happen."); messaging.logError("No children for " + parentNoteId + ". This shouldn't happen.");
return; return;
} }
@ -201,7 +201,7 @@ const noteTree = (function() {
const parents = childToParents[childNoteId]; const parents = childToParents[childNoteId];
if (!parents) { if (!parents) {
console.error("No parents found for " + childNoteId); messaging.logError("No parents found for " + childNoteId);
return; return;
} }
@ -218,7 +218,7 @@ const noteTree = (function() {
break; break;
} }
else { else {
console.log("No parents, can't activate node."); messaging.logError("No parents, can't activate node.");
return; return;
} }
} }

View file

@ -20,8 +20,20 @@ function init(httpServer, sessionParser) {
server: httpServer server: httpServer
}); });
webSocketServer.on('connection', function connection(ws, req) { webSocketServer.on('connection', (ws, req) => {
console.log("websocket client connected"); console.log("websocket client connected");
ws.on('message', messageJson => {
const message = JSON.parse(messageJson);
if (message.type === 'log-error') {
log.error('JS Error: ' + message.error);
}
else {
log.error('Unrecognized message: ');
log.error(message);
}
});
}); });
} }