From a0bbd8c8531b715517a47852dd3f24a559e495ef Mon Sep 17 00:00:00 2001 From: azivner Date: Wed, 6 Dec 2017 19:53:23 -0500 Subject: [PATCH] throwException instead of throwing exceptions manually (includes stacktrace) --- public/javascripts/context_menu.js | 4 ++-- public/javascripts/note_tree.js | 4 ++-- public/javascripts/utils.js | 4 ++++ services/data_encryption.js | 2 +- services/migration.js | 2 +- services/notes.js | 2 +- services/options.js | 2 +- services/request_context.js | 2 +- services/sync.js | 6 +++--- 9 files changed, 16 insertions(+), 12 deletions(-) diff --git a/public/javascripts/context_menu.js b/public/javascripts/context_menu.js index 9f5043363..59e498479 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 8fa2d537d..5f21c7cdb 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 030ba1e8b..97d5f9843 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 31ebf9044..84b8174e9 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 ce6e88e56..5a02cb05c 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 95e8cc4fb..c57d0d05a 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 bac7f6da8..a14b2d9ed 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 bb12d6ff7..ce43d926f 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 91d350eb3..92d8964b3 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); } }