From cba9d8b5c116864f7d1b6cc7270d14977594fe11 Mon Sep 17 00:00:00 2001 From: azivner Date: Fri, 1 Dec 2017 22:28:22 -0500 Subject: [PATCH] logging JS errors to backend logs --- public/javascripts/context_menu.js | 2 +- public/javascripts/dialogs/jump_to_note.js | 2 +- public/javascripts/init.js | 25 +++++++++++++++++++++- public/javascripts/messaging.js | 21 ++++++++++++++---- public/javascripts/note_tree.js | 6 +++--- services/messaging.js | 14 +++++++++++- 6 files changed, 59 insertions(+), 11 deletions(-) diff --git a/public/javascripts/context_menu.js b/public/javascripts/context_menu.js index fdf73e244..9f5043363 100644 --- a/public/javascripts/context_menu.js +++ b/public/javascripts/context_menu.js @@ -121,7 +121,7 @@ const contextMenu = (function() { treeChanges.deleteNode(node); } else { - console.log("Unknown command: " + ui.cmd); + messaging.logError("Unknown command: " + ui.cmd); } } }; diff --git a/public/javascripts/dialogs/jump_to_note.js b/public/javascripts/dialogs/jump_to_note.js index ad115d69a..b6f5a0149 100644 --- a/public/javascripts/dialogs/jump_to_note.js +++ b/public/javascripts/dialogs/jump_to_note.js @@ -80,7 +80,7 @@ const jumpToNote = (function() { dialogEl.dialog("close"); } else { - console.error("Unknown action=" + action); + messaging.logError("Unknown action=" + action); } return false; diff --git a/public/javascripts/init.js b/public/javascripts/init.js index 05fe68495..da5d72052 100644 --- a/public/javascripts/init.js +++ b/public/javascripts/init.js @@ -125,4 +125,27 @@ function showAppIfHidden() { // Kick off the CSS transition loaderDiv.style.opacity = 0.0; } -} \ No newline at end of file +} + +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; +}; \ No newline at end of file diff --git a/public/javascripts/messaging.js b/public/javascripts/messaging.js index 2e2b77167..f0977a40c 100644 --- a/public/javascripts/messaging.js +++ b/public/javascripts/messaging.js @@ -1,9 +1,20 @@ "use strict"; const messaging = (function() { - function messageHandler(event) { - console.log(event.data); + let ws = null; + 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); if (message.type === 'sync') { @@ -27,8 +38,6 @@ const messaging = (function() { } } - let ws = null; - function connectWebSocket() { // use wss for secure messaging ws = new WebSocket("ws://" + location.host); @@ -65,4 +74,8 @@ const messaging = (function() { showMessage("Re-connected to server"); } }, 3000); + + return { + logError + }; })(); \ No newline at end of file diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js index 19b855df7..4ef6e0756 100644 --- a/public/javascripts/note_tree.js +++ b/public/javascripts/note_tree.js @@ -144,7 +144,7 @@ const noteTree = (function() { function prepareNoteTreeInner(parentNoteId) { const childNoteIds = parentToChildren[parentNoteId]; if (!childNoteIds) { - console.log("No children for " + parentNoteId + ". This shouldn't happen."); + messaging.logError("No children for " + parentNoteId + ". This shouldn't happen."); return; } @@ -201,7 +201,7 @@ const noteTree = (function() { const parents = childToParents[childNoteId]; if (!parents) { - console.error("No parents found for " + childNoteId); + messaging.logError("No parents found for " + childNoteId); return; } @@ -218,7 +218,7 @@ const noteTree = (function() { break; } else { - console.log("No parents, can't activate node."); + messaging.logError("No parents, can't activate node."); return; } } diff --git a/services/messaging.js b/services/messaging.js index 9621d6d9f..c6e5842df 100644 --- a/services/messaging.js +++ b/services/messaging.js @@ -20,8 +20,20 @@ function init(httpServer, sessionParser) { server: httpServer }); - webSocketServer.on('connection', function connection(ws, req) { + webSocketServer.on('connection', (ws, req) => { 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); + } + }); }); }