Skip to content

Build your personal knowledge base with Trilium Notes

throwException instead of throwing exceptions manually (includes stacktrace)

azivnera0bbd8c8531b
diff --git a/public/javascripts/context_menu.js b/public/javascripts/context_menu.js
index 9f50433632..59e4984793 100644
--- a/public/javascripts/context_menu.js
+++ b/public/javascripts/context_menu.js
@@ -19,7 +19,7 @@ const contextMenu = (function() {
// just do nothing
}
else {
- throw new Error("Unrecognized clipboard mode=" + clipboardMode);
+ throwError("Unrecognized clipboard mode=" + clipboardMode);
}
clipboardId = null;
@@ -36,7 +36,7 @@ const contextMenu = (function() {
treeChanges.cloneNoteTo(clipboardId, node.data.note_id);
}
else {
- throw new Error("Unrecognized clipboard mode=" + mode);
+ throwError("Unrecognized clipboard mode=" + mode);
}
clipboardId = null;
diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js
index 8fa2d537d3..5f21c7cdb0 100644
--- a/public/javascripts/note_tree.js
+++ b/public/javascripts/note_tree.js
@@ -25,7 +25,7 @@ const noteTree = (function() {
let title = noteIdToTitle[noteId];
if (!title) {
- throw new Error("Can't find title for noteId='" + noteId + "'");
+ throwError("Can't find title for noteId='" + noteId + "'");
}
if (parentNoteId !== null) {
@@ -265,7 +265,7 @@ const noteTree = (function() {
const parents = childToParents[noteId];
if (!parents) {
- throw new Error("Can't find parents for noteId=" + noteId);
+ throwError("Can't find parents for noteId=" + noteId);
}
if (parents.length <= 1) {
diff --git a/public/javascripts/utils.js b/public/javascripts/utils.js
index 030ba1e8b9..97d5f9843c 100644
--- a/public/javascripts/utils.js
+++ b/public/javascripts/utils.js
@@ -30,6 +30,10 @@ function showError(message) {
});
}
+function throwError(message) {
+ throw new Error(message + ':' + new Error().stack);
+}
+
function getDateFromTS(timestamp) {
// Date accepts number of milliseconds since epoch so UTC timestamp works without any extra handling
// see https://stackoverflow.com/questions/4631928/convert-utc-epoch-to-local-date-with-javascript
diff --git a/services/data_encryption.js b/services/data_encryption.js
index 31ebf90443..84b8174e92 100644
--- a/services/data_encryption.js
+++ b/services/data_encryption.js
@@ -31,7 +31,7 @@ function pad(data) {
function encrypt(key, iv, plainText) {
if (!key) {
- throw new Error("No data key!");
+ throwError("No data key!");
}
const plainTextBuffer = Buffer.from(plainText);
diff --git a/services/migration.js b/services/migration.js
index ce6e88e56a..5a02cb05cc 100644
--- a/services/migration.js
+++ b/services/migration.js
@@ -58,7 +58,7 @@ async function migrate() {
await migrationModule(db);
}
else {
- throw new Error("Unknown migration type " + mig.type);
+ throwError("Unknown migration type " + mig.type);
}
await options.setOption("db_version", mig.dbVersion);
diff --git a/services/notes.js b/services/notes.js
index 95e8cc4fb0..c57d0d05ad 100644
--- a/services/notes.js
+++ b/services/notes.js
@@ -28,7 +28,7 @@ async function createNewNote(parentNoteId, note) {
await sync_table.addNoteReorderingSync(parentNoteId);
}
else {
- throw new Error('Unknown target: ' + note.target);
+ throwError('Unknown target: ' + note.target);
}
diff --git a/services/options.js b/services/options.js
index bac7f6da80..a14b2d9ede 100644
--- a/services/options.js
+++ b/services/options.js
@@ -10,7 +10,7 @@ async function getOption(optName) {
const row = await sql.getSingleResultOrNull("SELECT opt_value FROM options WHERE opt_name = ?", [optName]);
if (!row) {
- throw new Error("Option " + optName + " doesn't exist");
+ throwError("Option " + optName + " doesn't exist");
}
return row['opt_value'];
diff --git a/services/request_context.js b/services/request_context.js
index bb12d6ff71..ce43d926ff 100644
--- a/services/request_context.js
+++ b/services/request_context.js
@@ -9,7 +9,7 @@ module.exports = function(req) {
function getDataKey() {
if (!isProtectedSessionAvailable()) {
- throw new Error("Protected session is not available");
+ throwError("Protected session is not available");
}
return protected_session.getDataKey(req);
diff --git a/services/sync.js b/services/sync.js
index 91d350eb3a..92d8964b3d 100644
--- a/services/sync.js
+++ b/services/sync.js
@@ -157,7 +157,7 @@ async function pullSync(syncContext) {
await syncUpdate.updateRecentNotes(resp, syncContext.sourceId);
}
else {
- throw new Error("Unrecognized entity type " + sync.entity_name);
+ throwError("Unrecognized entity type " + sync.entity_name);
}
await setLastSyncedPull(sync.id);
@@ -228,7 +228,7 @@ async function readAndPushEntity(sync, syncContext) {
entity = await sql.getSingleResult('SELECT * FROM recent_notes WHERE note_tree_id = ?', [sync.entity_id]);
}
else {
- throw new Error("Unrecognized entity type " + sync.entity_name);
+ throwError("Unrecognized entity type " + sync.entity_name);
}
if (!entity) {
@@ -302,7 +302,7 @@ async function syncRequest(syncContext, method, uri, body) {
return await rp(options);
}
catch (e) {
- throw new Error("Request to " + method + " " + fullUri + " failed, inner exception: " + e.stack);
+ throwError("Request to " + method + " " + fullUri + " failed, inner exception: " + e.stack);
}
}