Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
error handling in custom request handler
zadambad7b8499325
diff --git a/src/public/stylesheets/desktop.css b/src/public/stylesheets/desktop.cssindex 38b7182df5..b3062f51c3 100644--- a/src/public/stylesheets/desktop.css+++ b/src/public/stylesheets/desktop.css@@ -102,6 +102,6 @@ body {}::-webkit-scrollbar-thumb {- border-radius: 5px;- -webkit-box-shadow: inset 0 0 3px var(--main-border-color);+ border-radius: 3px;+ border: 1px solid var(--main-border-color);}\ No newline at end of filediff --git a/src/public/stylesheets/style.css b/src/public/stylesheets/style.cssindex cc3455bb75..b460c32d1f 100644--- a/src/public/stylesheets/style.css+++ b/src/public/stylesheets/style.css@@ -9,7 +9,7 @@--main-background-color: white;--main-text-color: black;- --main-border-color: #ddd;+ --main-border-color: #ccc;--accented-background-color: #eee;--more-accented-background-color: #ccc;--header-background-color: #f8f8f8;@@ -345,6 +345,10 @@ div.ui-tooltip {padding: 5px 5px 5px 15px;}+#search-text {+ border: 1px solid var(--main-border-color);+}+/** .electron-in-page-search-window is a class specified to default* <webview> element for search window.diff --git a/src/routes/custom.js b/src/routes/custom.jsindex ec91770b8c..4b769a125f 100644--- a/src/routes/custom.js+++ b/src/routes/custom.js@@ -31,11 +31,18 @@ function register(router) {log.info(`Handling custom request "${path}" with note ${note.noteId}`);- await scriptService.executeNote(note, {- pathParams: match.slice(1),- req,- res- });+ try {+ await scriptService.executeNote(note, {+ pathParams: match.slice(1),+ req,+ res+ });+ }+ catch (e) {+ log.error(`Custom handler ${note.noteId} failed with ${e.message}`);++ res.status(500).send(e.message);+ }}else if (attr.name === 'customResourceProvider') {await fileUploadService.downloadNoteFile(attr.noteId, res);diff --git a/src/services/backend_script_api.js b/src/services/backend_script_api.jsindex f30a84234e..5347bcfbfa 100644--- a/src/services/backend_script_api.js+++ b/src/services/backend_script_api.js@@ -173,6 +173,23 @@ function BackendScriptApi(currentNote, apiParams) {*/this.createNote = noteService.createNote;+ /**+ * Creates new note according to given params and force all connected clients to refresh their tree.+ *+ * @method+ *+ * @param {string} parentNoteId - create new note under this parent+ * @param {string} title+ * @param {string} [content=""]+ * @param {CreateNoteExtraOptions} [extraOptions={}]+ * @returns {Promise<{note: Note, branch: Branch}>} object contains newly created entities note and branch+ */+ this.createNoteAndRefresh = async function(parentNoteId, title, content, extraOptions) {+ await noteService.createNote(parentNoteId, title, content, extraOptions);++ messagingService.refreshTree();+ };+/*** Log given message to trilium logs.*@@ -238,7 +255,7 @@ function BackendScriptApi(currentNote, apiParams) {** @returns {Promise<void>}*/- this.refreshTree = () => messagingService.sendMessageToAllClients({ type: 'refresh-tree' });+ this.refreshTree = messagingService.refreshTree;/*** @return {{syncVersion, appVersion, buildRevision, dbVersion, dataDirectory, buildDate}|*} - object representing basic info about running Trilium versiondiff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.jsindex 5152d5251a..d4bc19fd2a 100644--- a/src/services/consistency_checks.js+++ b/src/services/consistency_checks.js@@ -432,7 +432,7 @@ async function runChecks() {});if (fixedIssues) {- messagingService.sendMessageToAllClients({ type: 'refresh-tree' });+ messagingService.refreshTree();}if (unrecoverableConsistencyErrors) {diff --git a/src/services/handlers.js b/src/services/handlers.jsindex 95973be86b..3d776ca12d 100644--- a/src/services/handlers.js+++ b/src/services/handlers.js@@ -12,7 +12,7 @@ async function runAttachedRelations(note, relationName, originEntity) {const scriptNote = await relation.getTargetNote();if (scriptNote) {- await scriptService.executeNote(scriptNote, { originEntity });+ await scriptService.executeNoteNoException(scriptNote, { originEntity });}else {log.error(`Target note ${relation.value} of atttribute ${relation.attributeId} has not been found.`);@@ -30,7 +30,7 @@ eventService.subscribe(eventService.NOTE_TITLE_CHANGED, async note => {if (await parent.hasLabel("sorted")) {await treeService.sortNotesAlphabetically(parent.noteId);- messagingService.sendMessageToAllClients({ type: 'refresh-tree' });+ messagingService.refreshTree();break; // sending the message once is enough}}diff --git a/src/services/messaging.js b/src/services/messaging.jsindex 2ac650ac2e..6006e02b52 100644--- a/src/services/messaging.js+++ b/src/services/messaging.js@@ -49,6 +49,10 @@ async function sendMessage(client, message) {}}+async function refreshTree() {+ await sendMessageToAllClients({ type: 'refresh-tree' });+}+async function sendMessageToAllClients(message) {const jsonStr = JSON.stringify(message);@@ -76,5 +80,6 @@ async function sendPing(client, lastSentSyncId) {module.exports = {init,+ refreshTree,sendMessageToAllClients};\ No newline at end of filediff --git a/src/services/scheduler.js b/src/services/scheduler.jsindex 35e3e9ec8b..4e19a605a1 100644--- a/src/services/scheduler.js+++ b/src/services/scheduler.js@@ -17,7 +17,7 @@ async function runNotesWithLabel(runAttrValue) {AND notes.isDeleted = 0`, [runAttrValue]);for (const note of notes) {- scriptService.executeNote(note, { originEntity: note });+ scriptService.executeNoteNoException(note, { originEntity: note });}}diff --git a/src/services/script.js b/src/services/script.jsindex 21bc81884b..596213a843 100644--- a/src/services/script.js+++ b/src/services/script.js@@ -15,6 +15,15 @@ async function executeNote(note, apiParams) {await executeBundle(bundle, apiParams);}+async function executeNoteNoException(note, apiParams) {+ try {+ await executeNote(note, apiParams);+ }+ catch (e) {+ // just swallow, exception is logged already in executeNote+ }+}+async function executeBundle(bundle, apiParams = {}) {if (!apiParams.startNote) {// this is the default case, the only exception is when we want to preserve frontend startNote@@ -36,6 +45,8 @@ async function executeBundle(bundle, apiParams = {}) {}catch (e) {log.error(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`);++ throw e;}}@@ -168,6 +179,7 @@ function sanitizeVariableName(str) {module.exports = {executeNote,+ executeNoteNoException,executeScript,getScriptBundleForFrontend};\ No newline at end of filediff --git a/src/views/desktop.ejs b/src/views/desktop.ejsindex 2d9d1747a4..edd7506ade 100644--- a/src/views/desktop.ejs+++ b/src/views/desktop.ejs@@ -76,7 +76,7 @@<div id="search-box"><div style="display: flex; align-items: center; flex-wrap: wrap;">- <input name="search-text" placeholder="Search text, labels" style="flex-grow: 100; margin-left: 5px; margin-right: 5px; flex-basis: 5em; min-width: 0;" autocomplete="off">+ <input name="search-text" id="search-text" placeholder="Search text, labels" style="flex-grow: 100; margin-left: 5px; margin-right: 5px; flex-basis: 5em; min-width: 0;" autocomplete="off"><button id="do-search-button" class="btn btn-sm icon-button jam jam-search" title="Search (enter)"></button>