Skip to content

Build your personal knowledge base with Trilium Notes

logging JS errors to backend logs

azivnercba9d8b5c116
diff --git a/public/javascripts/context_menu.js b/public/javascripts/context_menu.js
index fdf73e244b..9f50433632 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 ad115d69a5..b6f5a0149d 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 05fe68495d..da5d720521 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 2e2b771670..f0977a40cc 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 19b855df77..4ef6e07567 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 9621d6d9f7..c6e5842dfe 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);
+ }
+ });
});
}