mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
better logging and notifications on script errors for easier debugging
This commit is contained in:
parent
513748836e
commit
145efe67c3
@ -1,5 +1,6 @@
|
|||||||
import ScriptContext from "./script_context.js";
|
import ScriptContext from "./script_context.js";
|
||||||
import server from "./server.js";
|
import server from "./server.js";
|
||||||
|
import infoService from "./info.js";
|
||||||
|
|
||||||
async function getAndExecuteBundle(noteId, originEntity = null) {
|
async function getAndExecuteBundle(noteId, originEntity = null) {
|
||||||
const bundle = await server.get('script/bundle/' + noteId);
|
const bundle = await server.get('script/bundle/' + noteId);
|
||||||
@ -10,10 +11,15 @@ async function getAndExecuteBundle(noteId, originEntity = null) {
|
|||||||
async function executeBundle(bundle, originEntity) {
|
async function executeBundle(bundle, originEntity) {
|
||||||
const apiContext = ScriptContext(bundle.note, bundle.allNotes, originEntity);
|
const apiContext = ScriptContext(bundle.note, bundle.allNotes, originEntity);
|
||||||
|
|
||||||
|
try {
|
||||||
return await (function () {
|
return await (function () {
|
||||||
return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);
|
return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);
|
||||||
}.call(apiContext));
|
}.call(apiContext));
|
||||||
}
|
}
|
||||||
|
catch (e) {
|
||||||
|
infoService.showAndLogError(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function executeStartupBundles() {
|
async function executeStartupBundles() {
|
||||||
const scriptBundles = await server.get("script/startup");
|
const scriptBundles = await server.get("script/startup");
|
||||||
|
@ -14,6 +14,12 @@ function showMessage(message) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function showAndLogError(message, delay = 10000) {
|
||||||
|
showError(message, delay);
|
||||||
|
|
||||||
|
messagingService.logError(message);
|
||||||
|
}
|
||||||
|
|
||||||
function showError(message, delay = 10000) {
|
function showError(message, delay = 10000) {
|
||||||
console.log(utils.now(), "error: ", message);
|
console.log(utils.now(), "error: ", message);
|
||||||
|
|
||||||
@ -36,5 +42,6 @@ function throwError(message) {
|
|||||||
export default {
|
export default {
|
||||||
showMessage,
|
showMessage,
|
||||||
showError,
|
showError,
|
||||||
|
showAndLogError,
|
||||||
throwError
|
throwError
|
||||||
}
|
}
|
@ -72,8 +72,13 @@ function ScriptApi(startNote, currentNote, originEntity = null) {
|
|||||||
originEntityId: originEntity ? originEntity.noteId : null
|
originEntityId: originEntity ? originEntity.noteId : null
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (ret.success) {
|
||||||
return ret.executionResult;
|
return ret.executionResult;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
throw new Error("server error: " + ret.error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
startNote: startNote,
|
startNote: startNote,
|
||||||
|
@ -5,10 +5,15 @@ const attributeService = require('../../services/attributes');
|
|||||||
const repository = require('../../services/repository');
|
const repository = require('../../services/repository');
|
||||||
|
|
||||||
async function exec(req) {
|
async function exec(req) {
|
||||||
|
try {
|
||||||
const result = await scriptService.executeScript(req.body.script, req.body.params, req.body.startNoteId,
|
const result = await scriptService.executeScript(req.body.script, req.body.params, req.body.startNoteId,
|
||||||
req.body.currentNoteId, req.body.originEntityName, req.body.originEntityId);
|
req.body.currentNoteId, req.body.originEntityName, req.body.originEntityId);
|
||||||
|
|
||||||
return { executionResult: result };
|
return { success: true, executionResult: result };
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
return { success: false, error: e.message };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function run(req) {
|
async function run(req) {
|
||||||
|
@ -3,6 +3,7 @@ const ScriptContext = require('./script_context');
|
|||||||
const repository = require('./repository');
|
const repository = require('./repository');
|
||||||
const cls = require('./cls');
|
const cls = require('./cls');
|
||||||
const sourceIdService = require('./source_id');
|
const sourceIdService = require('./source_id');
|
||||||
|
const log = require('./log');
|
||||||
|
|
||||||
async function executeNote(note, originEntity) {
|
async function executeNote(note, originEntity) {
|
||||||
if (!note.isJavaScript()) {
|
if (!note.isJavaScript()) {
|
||||||
@ -25,6 +26,7 @@ async function executeBundle(bundle, startNote, originEntity = null) {
|
|||||||
|
|
||||||
const ctx = new ScriptContext(startNote, bundle.allNotes, originEntity);
|
const ctx = new ScriptContext(startNote, bundle.allNotes, originEntity);
|
||||||
|
|
||||||
|
try {
|
||||||
if (await bundle.note.hasLabel('manualTransactionHandling')) {
|
if (await bundle.note.hasLabel('manualTransactionHandling')) {
|
||||||
return await execute(ctx, script, '');
|
return await execute(ctx, script, '');
|
||||||
}
|
}
|
||||||
@ -32,6 +34,10 @@ async function executeBundle(bundle, startNote, originEntity = null) {
|
|||||||
return await sql.transactional(async () => await execute(ctx, script, ''));
|
return await sql.transactional(async () => await execute(ctx, script, ''));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (e) {
|
||||||
|
log.error(`Execution of script "${bundle.note.title}" (${bundle.note.noteId}) failed with error: ${e.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method preserves frontend startNode - that's why we start execution from currentNote and override
|
* This method preserves frontend startNode - that's why we start execution from currentNote and override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user