error handling in custom request handler

This commit is contained in:
zadam 2019-02-03 11:15:32 +01:00
parent d3ca6b5ae6
commit bad7b84993
10 changed files with 59 additions and 14 deletions

View file

@ -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);
}

View file

@ -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.

View file

@ -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);

View file

@ -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 version

View file

@ -432,7 +432,7 @@ async function runChecks() {
});
if (fixedIssues) {
messagingService.sendMessageToAllClients({ type: 'refresh-tree' });
messagingService.refreshTree();
}
if (unrecoverableConsistencyErrors) {

View file

@ -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
}
}

View file

@ -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
};

View file

@ -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 });
}
}

View file

@ -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
};

View file

@ -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>
&nbsp;