throwException instead of throwing exceptions manually (includes stacktrace)

This commit is contained in:
azivner 2017-12-06 19:53:23 -05:00
parent a7831ebfcd
commit a0bbd8c853
9 changed files with 16 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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'];

View File

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

View File

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