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

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